PHP如何使用元素名称将json格式映射为something:something:something [英] PHP how to map json with element name in form something:something:something
问题描述
我正在尝试将json文件映射到mysql数据库,请参见未定义的索引名称以获取代码
I am trying to map a json file to a mysql database see undefined index Name for code
我认为的问题是,第3个条目中的任何一个元素都没有值,但是有一个名为aws:autoscaling:groupName的值
the problem I believe is that there is no value for either element from the 3rd entry but there is a value called aws:autoscaling:groupName
我只是想知道应该如何声明这是一样的还是冒号有所作为,因为由于没有按照上一个问题找到索引而导致在上述问题中使用该方法失败
I am just wondering how I should go about declaring this is it the same or does the colons make a difference as using the method in the aforementioned question fails due to not finding index as per the previous question thanks
我的json或至少重要的位
my json or at least the important bits
{
"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);
}
}
}
?>
从标签下方的第3个条目开始,没有名称或ID
from the 3rd entry in tags down there is no name or id only
aws:autoscaling:groupName":"ESND-PROD-US-14-02-14"
我将此行添加到了php
I added this line to my php
$awsautoscalinggroupName= $configurationItems["tags"] ["aws:autoscaling:groupName"];
推荐答案
在我看来,您可以执行以下操作:
Here is what you can do in my opinion :
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代码:
Edit your php code as follow :
$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:something的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!