嗨,我目前正在研究剥离一个 json 文件并将元素放入数据库中,使用以下代码在 json 文件中有 26 个条目我已经能够映射大多数条目,但现在处于 json 中的某些元素仅出现在某些条目中的位置,例如前 2 个条目中的标签具有名称和cirrushq_id字段,但来自第 3 个条目只有一个 aws:autoscaling:groupName 在前 2 个条目中不存在,所以我的问题实际上是如何修改 php,以便它考虑到可能没有每个条目的名称或 id 并且随后不会中断?
这是 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 />";
$awsautoscalinggroupName= $configurationItems["tags"]["aws:autoscaling:groupName"];
echo "aws:autoscaling:groupName:",$awsautoscalinggroupName,"<br />";
$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id, aws_autoscaling_group_name)
VALUES('$Name','$cirrushq_id', '$awsautoscalinggroupName')")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);
}
}
}
?>
和 JSON
{
"fileVersion":"1.0",
"configurationItems":[
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"",
"awsRegion":"us-east-1",
"availabilityZone":"us-east-1b",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-01-06T10:37:37.000Z",
"tags":{
"Name":"dbn.prod-us.wordeo.com",
"cirrushq_id":"instance_20"
},
与上述第二次进入
第 3 个条目与标签相同,如下所示
"tags":{
"aws:autoscaling:groupName":"ES-PROD-US-03-01-14"
},
根据答案使用 ISSET 方法
<?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++)
$tags=$configurationItems["tags"];
if(isset($tags["Name"]) && isset($tags["cirrushq_id"])){
$Name=$tags["Name"];
echo "Name:",$Name,"<br />";
$cirrushq_id=$tags["cirrushq_id"];
echo "cirrushq_id:",$cirrushq_id,"<br />";
$awsautoscalinggroupName= isset($tags["aws:autoscaling:groupName"]) ?
$tags["aws:autoscaling:groupName"] : '';
echo "aws:autoscaling:groupName:",$awsautoscalinggroupName,"<br />";
$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id, aws_autoscaling_group_name)
VALUES('$Name','$cirrushq_id', '$awsautoscalinggroupName')");
}// 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);
}
}
}
?>
使用 isset() 检查名称或 id 是否存在。更改 php foreach 循环代码,如下所示,
$tags=$configurationItems["tags"];
if(isset($tags["Name"]) && isset($tags["cirrushq_id"])){
$Name=$tags["Name"];
echo "Name:",$Name,"<br />";
$cirrushq_id=$tags["cirrushq_id"];
echo "cirrushq_id:",$cirrushq_id,"<br />";
$awsautoscalinggroupName= isset($tags["aws:autoscaling:groupName"]) ?
$tags["aws:autoscaling:groupName"] : '';
echo "aws:autoscaling:groupName:",$awsautoscalinggroupName,"<br />";
// remove the die if there is any error then it will go forward
$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id, aws_autoscaling_group_name)
VALUES('$Name','$cirrushq_id', '$awsautoscalinggroupName')");