JSON解码文件中的元素

时间:2015-01-20 12:14:10

标签: php json

您好我正在努力剥离一个json文件并将元素放入数据库中json文件中有26个条目使用以下代码我已经能够映射大部分条目但我现在处于某个位置因此,json中的某些元素只出现在某些条目中,例如前两个条目中的标签有一个Name和cirrushq_id字段,但是从第三个条目向下只有一个aws:autoscaling:groupName不在前两个条目中,所以我的问题确实是如何修改php,以便考虑到每个条目可能没有Name或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); 
} 

} 
} 

&GT?;

1 个答案:

答案 0 :(得分:1)

使用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')");