gpt4 book ai didi

PHP如何将json与元素名称映射到表单中的某些内容:something:something

转载 作者:行者123 更新时间:2023-11-29 12:27:50 25 4
gpt4 key购买 nike

我正在尝试将 json 文件映射到 mysql 数据库,请参阅 undefined index Name对于代码

我认为的问题是第三个条目中的任何一个元素都没有值,但有一个名为 aws:autoscaling:groupName 的值

我只是想知道我应该如何声明这是相同的还是冒号有什么不同,因为使用上述问题中的方法由于没有按照上一个问题找到索引而失败,谢谢

我的 json 或至少是重要的部分

{  
"fileVersion":"1.0",
"configurationItems":[
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"",
"availabilityZone":"",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-01-06T10:37:37.000Z",
"tags":{
"Name":"dbn.prod-us.wordeo.com",
"cirrushq_id":"instance_20"
},
"relationships":[ ],
"configuration":{ }
},
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"",
"availabilityZone":"",
"configurationStateMd5Hash":"",
"resourceType":"",
"resourceCreationTime":"",
"tags":{
"Name":"db-backup.prod-us.wordeo.com",
"cirrushq_id":"instance_7701"
},
"relationships":[ ],
"configuration":{ }
},
{ },
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"",
"availabilityZone":"",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-09-29T07:25:44.000Z",
"tags":{
"aws:autoscaling:groupName":"ESND-PROD-US-14-02-14"
},
"relationships":[ ],

这是 php

<?php 

$con=mysqli_connect("localhost","root","","json_map");
$response = array();
$res=array();
$json = file_get_contents('C:\Users\Richard\Desktop\test.json');

if($json!=null){
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];

if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)

{
$Name=$configurationItems["tags"]["Name"];
echo "Name:",$Name,"<br />";

$cirrushq_id=$configurationItems["tags"]["cirrushq_id"];
echo "cirrushq_id:",$cirrushq_id,"<br />";

$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id) VALUES('$Name','$cirrushq_id')")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;

}// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored tags ";

// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";

// echoing JSON response
echo json_encode($response);
}

}
}

?>

从标签中的第三个条目开始,只有名称或 ID

aws:autoscaling:groupName":"ESND-PROD-US-14-02-14" 

我将此行添加到我的 php

 $awsautoscalinggroupName= $configurationItems["tags"]  ["aws:autoscaling:groupName"];

最佳答案

我认为您可以执行以下操作:

1 - Add an extra field to your table (Tag), like 'group_name' or something.
2 - Make All the fields nullable, (name, cirrushq_id, group_name).

编辑您的 php 代码如下:

$Name= isset($configurationItems["tags"]["Name"]) ? $configurationItems["tags"]["Name"] : ''; 
$cirrushq_id=isset($configurationItems["tags"]["cirrushq_id"]) ? $configurationItems["tags"]["cirrushq_id"] : '';
$group_name=isset($configurationItems["tags"]["aws:autoscaling:groupName"]) ? $configurationItems["tags"]["aws:autoscaling:groupName"] : '';

$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id, group_name) VALUES('$Name','$cirrushq_id', '$group_name')") or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;

关于PHP如何将json与元素名称映射到表单中的某些内容:something:something,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com