我正在尝试导入JSON文件,读取它然后输出结果。我对获得JSON密钥等感到困惑。
这是JSON文件的摘录:
{
"CVE_data_type" : "CVE",
"CVE_data_format" : "MITRE",
"CVE_data_version" : "4.0",
"CVE_data_numberOfCVEs" : "566",
"CVE_data_timestamp" : "2017-07-26T10:00Z",
"CVE_Items" : [ {
"cve" : {
"CVE_data_meta" : {
"ID" : "CVE-2010-1154"
},
"affects" : {
"vendor" : {
"vendor_data" : [ ]
}
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ ]
} ]
},
"references" : {
"reference_data" : [ ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "** REJECT ** DO NOT USE THIS CANDIDATE NUMBER. ConsultIDs: none. Reason: This candidate was in a CNA pool that was not assigned to any issues during 2010. Notes: none."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ ]
},
"impact" : { },
"publishedDate" : "2017-07-24T16:29Z",
"lastModifiedDate" : "2017-07-24T16:29Z"
}, {
"cve" : {
"CVE_data_meta" : {
"ID" : "CVE-2010-1430"
},
"affects" : {
"vendor" : {
"vendor_data" : [ ]
}
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ ]
} ]
},
"references" : {
"reference_data" : [ ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "** REJECT ** DO NOT USE THIS CANDIDATE NUMBER. ConsultIDs: none. Reason: This candidate was in a CNA pool that was not assigned to any issues during 2010. Notes: none."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ ]
},
"impact" : { },
"publishedDate" : "2017-07-24T16:29Z",
"lastModifiedDate" : "2017-07-24T16:29Z"
}, {
"cve" : {
"CVE_data_meta" : {
"ID" : "CVE-2010-1631"
},
"affects" : {
"vendor" : {
"vendor_data" : [ ]
}
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ ]
} ]
},
"references" : {
"reference_data" : [ ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "** REJECT ** DO NOT USE THIS CANDIDATE NUMBER. ConsultIDs: none. Reason: This candidate was in a CNA pool that was not assigned to any issues during 2010. Notes: none."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ ]
},
"impact" : { },
"publishedDate" : "2017-07-24T16:29Z",
"lastModifiedDate" : "2017-07-24T16:29Z"
},
我的PHP文件如下:
<?php
//Load the JSON feed
$file = 'nvdcve-1.0-recent.json';
$json = file_get_contents($file);
$json_data = json_decode($json, true);
$vulns = $json_data->CVE_Items[0];
//Check if JSON file is actually found.
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
//echo '<pre>' . var_dump($json_datas) . '</pre>';
//Loop through items and display
foreach ($vulns as $vuln) {
$cveid = $vuln->cve->CVE_data_meta->ID;
echo $cveid . '<br>';
}
//Print the array - Debugging only.
//print_r($json_datas);
?>
这个PHP仅用于测试,最终,我需要循环并回显所有不同的部分,以创建完整的条目,如ID,影响,问题类型,引用等......
我以前从未在PHP上使用JSON,今天我读了很多文章,每个人都告诉我一个不同的方式,这让我头疼!
答案 0 :(得分:-1)
你的json无效。这就是json_decode($ json)返回NULL的原因。 在这里查看json https://jsonformatter.curiousconcept.com/
更新
<?php
$file = 'nvdcve-1.0-recent.json';
$json = file_get_contents($file);
$json_data = json_decode($json);
$vulns = $json_data->CVE_Items;
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
foreach ($vulns as $vuln) {
$cveid = $vuln->cve->CVE_data_meta->ID;
echo $cveid; echo '<br>';
}
?>