JSON解码为多个表

时间:2015-01-15 10:24:23

标签: php mysql json

我的任务是尝试将json文件映射到数据库,下面给出了json的样本:

configurationItems":[  
  {  
     "configurationItemVersion":"1.0",
     "configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
     "configurationStateId":1,
     "relatedEvents":[  ],
     "awsAccountId":"val",
     "configurationItemStatus":"v",
     "resourceId":"fg",
     "ARN":"ggggg",
     "awsRegion":"us-east-1",
     "availabilityZone":"us-east-1b",
     "configurationStateMd5Hash":"ggd45",
     "resourceType":"AWS::EC2::Instance",
     "resourceCreationTime":"2014-01-06T10:37:37.000Z",
     "tags":{  
        "Name":"",
        "cirrushq_id":""
     },

使用以下代码我已经能够将条目映射到资源创建时间,但现在希望将标记映射到另一个名为tag的表

<?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++) 

 { 


$configurationItemVersion=$configurationItems["configurationItemVersion"]; 
echo "<br />","configuration_Item_Version:",$configurationItemVersion,"<br        />"; 

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

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

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

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

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

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


$awsRegion=$configurationItems["awsRegion"]; 
echo "awsRegion:",$awsRegion,"<br />"; 
 etc etc

  $result = mysqli_query($con, "INSERT INTO        configuration_item(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)


VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )")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 configuration items "; 

    // 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); 
} 

} 
} 



?> 

我尝试将代码复制到一个单独的文件中,并将insert语句改为

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

 { 


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

$cirrushq_id=$configurationItems["cirrushq_id"]; 
echo "<br />","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)));; 

但无济于事只会返回未知索引名称

1 个答案:

答案 0 :(得分:1)

根据你所写的内容,我并不完全确定你的问题是什么原因。

但是,让我指出我发现的一些不一致之处:

1)由于您谈论标签(复数),我猜您希望每个configurationItems元素都有多个标签。在这种情况下,您的JSON应该是:

...
"tags":[{
    "Name":"",
    "cirrushq_id":""
 },{
    "Name":"",
    "cirrushq_id":""
 }]
...

2)根据您的JSON,要访问标记名称和标记cirrushq_id,您应该这样做:

$Name=$configurationItems["tags"]["Name"];
$cirrushq_id=$configurationItems["tags"]["cirrushq_id"];

如果考虑到我的观点1),你必须在标签上再做一次循环:

foreach($decoded["configurationItems"] as $configurationItems)
{
...
   foreach($decoded["configurationItems"]["tags"] as $tags)
   { 
      $Name=$tags["Name"]; 
      echo "<br />","Name:",$Name,"<br />"; 

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

      // clearly, the INSERT query should also be inside this nested loop 
   }
}

3)根据您在标记INSERT查询中所写的内容,您有一个小写的name列,而在您的JSON中,索引Name是大写的...我知道它是但有时候,当没有另一双眼睛可以仔细检查你的代码时,这样的简单东西会让你浪费很多时间。

希望得到帮助!