您好我正在尝试剥离json文件的内容并映射到mysql数据库结构但是我遇到了“tags”值的问题,因为找不到值Name但仍然填充了2个条目数据库在正确的地方
这是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":"i-0f8a032c",
"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);
}
}
}
?>
答案 0 :(得分:1)
你的php代码工作正常,但你的json无效:
"configurationStateId":, => double quote missing.
使用双引号而不是单引号:
"ARN":", => ""