我正在尝试使用以下JSON解码多个文件中的JSON元素
configurationItems":[
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2015-01-12T16:44:03.358Z",
"configurationStateId":126,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDeleted",
"resourceId":"i-20faebca",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"tags":{ },
"relationships":[ ]
},
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2015-01-12T16:33:51.495Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"",
"configurationItemStatus":"ResourceDiscovered",
"resourceId":"",
"ARN":"arn:aws:ec2:us-east-1:037734006438:instance/i-231a43d8",
"awsRegion":"us-east-1",
"availabilityZone":"us-east-1c",
"configurationStateMd5Hash":"",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2015-01-12T16:29:22.000Z",
"tags":{ },
"relationships":[ ],
"configuration":{
"instanceId":"",
"imageId":"",
"state":{
"code":16,
"name":"running"
},
我知道我得到未定义索引错误的原因是因为第一个条目没有配置元素我的问题是如何在我的代码中处理这个?我的PHP脚本如下:
<?php
$con=mysqli_connect("localhost","root","","json_map");
$response = array();
$res=array();
$result='' ;
foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\views\*.json') as $filename) {$json = file_get_contents($filename);
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++)
$cfi= $configurationItems["configuration"];
if(isset($cfi["instanceId"]) && isset($cfi["imageId"])&& isset($cfi["privateDnsName"] //repeat for all elements))
{
// all defined and echoed as
$ebsOptimized=$configurationItems["configuration"]["ebsOptimized"];
echo "ebsOptimized:",$ebsOptimized,"<br />";
$result = mysqli_query($con, "INSERT INTO configuration(instance_id, image_id, private_dns_name, public_dns_name, key_name, state_transition_reason, ami_launch_index, instance_type, launch_time, kernel_id, subnet_id, vpc_id, private_ip_address,architecture, root_device_type, root_device_name, virtualisation_type, client_token, source_dest_check, hypervisor, ebs_optimised)
VALUES('$instanceId','$imageId', '$privateDnsName' , '$publicDnsName', '$keyName', '$stateTransitionReason', '$amiLaunchIndex', '$instanceType', '$launchTime', '$kernelId', '$subnetId', '$vpcId', '$privateIpAddress', '$architecture', '$rootDeviceType', '$rootDeviceName', '$virtualizationType', '$clientToken', '$sourceDestCheck', '$hypervisor', '$ebsOptimized')")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 updated config table ";
// 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
if(isset($array['yourindex'])){
//do your stuff
}
Javascript
if (typeof yourJsonObject.yourIndex != 'undefined') {
// do your stuff
}