我有一个非常大的JSON字符串(超过1mb)。它是这样开始的:
[[{ “时间戳”:1456815458, “total_jobs”:6398, “latest_height”:99045, “工作”:{“152.62.311.2:123":[8003,"/Main:5.1/",1655659260, 1,395047,“ec4.ap-southeast-3.amazonaws.com”,“Capetown”,“AU”, - 317.8815,57.5,“Australia / NSW”,“AS1511”,“Amazon.com,Inc。”] “183.215.34.58:9805”:56734, “/ JobPlace2:0.11.0 /”,1455767214,1,399047, “185.211.134.58”, “首尔”, “KR”,57.5983,126.9783,“亚洲/首尔”, “AS32665”, “HCLC”],...
我希望能够回显IP地址以及IP地址附带的信息(每个IP地址后面有大约13条子信息)。
我该怎么做?我之前从未在解码过的JSON字符串中访问类/对象。
为了给出所需PHP输出的示例,对于第一个IP地址(152.62.311.2:123),它应该输出:
8003
/Main:5.1/
1655659260个
1
395047个
ec4.ap-southeast-3.amazonaws.com
开普敦
AU
-317.8815
57.5
澳大利亚/新南威尔士州
AS1511
Amazon.com,Inc
答案 0 :(得分:0)
这可能不是最好的解决方案,但它是一个解决方案......
$decoded = json_decode($json);
$ipInfo= $decoded[0][0]->jobs->{'152.62.311.2:123'};
print_r($ipInfo);
jobs数组输出如下:
Array
(
[0] => 8003
[1] => /Main:5.1/
[2] => 1655659260
[3] => 1
[4] => 395047
[5] => ec4.ap-southeast-3.amazonaws.com
[6] => Capetown
[7] => AU
[8] => -317.8815
[9] => 57.5
[10] => Australia/NSW
[11] => AS1511
[12] => Amazon.com, Inc.
)
您可以在此处查看示例:https://ideone.com/bzhhLI
如果您纯粹想要遍历并打印出所有可以执行此操作的信息,尽管这不是一个非常漂亮的解决方案:
$json = '[
[{
"timestamp": 1456815458,
"total_jobs": 6398,
"latest_height": 99045,
"jobs": {
"152.62.311.2:123": [8003, "/Main:5.1/", 1655659260, 1, 395047, "ec4.ap-southeast-3.amazonaws.com", "Capetown", "AU", -317.8815, 57.5, "Australia/NSW", "AS1511", "Amazon.com, Inc."],
"183.215.34.58:9805": [56734, "/JobPlace2:0.11.0/", 1455767214, 1, 399047, "185.211.134.58", "Seoul", "KR", 57.5983, 126.9783, "Asia/Seoul", "AS32665", "HCLC"]
}
}]
]';
$decoded = json_decode($json);
$jobs = $decoded[0][0]->jobs->{'152.62.311.2:123'};
header('content-type: text/plain');
foreach ($decoded[0][0]->jobs as $ip => $ipData) {
echo $ip . PHP_EOL;
foreach ($ipData as $data) {
echo $data . PHP_EOL;
}
}
答案 1 :(得分:0)
$json_data = '[[{"timestamp": 1456815458,"total_jobs": 6398,"latest_height": 99045,"jobs": {"152.62.311.2:123": [8003,"/Main:5.1/", 1655659260, 1, 395047, "ec4.ap-southeast-3.amazonaws.com", "Capetown", "AU", -317.8815, 57.5, "Australia/NSW", "AS1511", "Amazon.com, Inc."],"183.215.34.58:9805": [56734, "/JobPlace2:0.11.0/", 1455767214, 1, 399047, "185.211.134.58", "Seoul", "KR", 57.5983, 126.9783, "Asia/Seoul", "AS32665", "HCLC"]}}]]';
$json_feed = json_decode($json_data);
foreach ($json_feed[0]->jobs as $ip => $ipData) {
echo "<br><b>IP Address: ". $ip . "</b><br>";
foreach ($ipData as $data) {
echo "Data: ". $data . "<br>";
}
}