我试图使用PHP解析JSON文件.但我现在被困住了.
这是我的JSON文件的内容:
{
"John": {
"status":"Wait"
},
"Jennifer": {
"status":"Active"
},
"James": {
"status":"Active",
"age":56,
"count":10,
"progress":0.0029857,
"bad":0
}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的:
<?php
$string = file_get_contents("/home/michael/test.json");
$json_a = json_decode($string, true);
echo $json_a['John'][status];
echo $json_a['Jennifer'][status];
Run Code Online (Sandbox Code Playgroud)
但是,因为我不知道的名字(例如'John'
,'Jennifer'
)和所有可用键和值(如'age'
,'count'
)事前,我想我需要创建一些foreach循环.
我会很感激这样的例子.
Gor*_*don 318
要迭代多维数组,可以使用RecursiveArrayIterator
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n";
} else {
echo "$key => $val\n";
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
John:
status => Wait
Jennifer:
status => Active
James:
status => Active
age => 56
count => 10
progress => 0.0029857
bad => 0
Run Code Online (Sandbox Code Playgroud)
Bol*_*ock 113
我不敢相信很多人在没有正确阅读JSON的情况下发布答案.
如果$json_a
单独使用迭代,则会有一个对象对象.即使您true
作为第二个参数传入,也有一个二维数组.如果你在第一个维度上循环,你就不能像这样回应第二个维度.所以这是错的:
foreach ($json_a as $k => $v) {
echo $k, ' : ', $v;
}
Run Code Online (Sandbox Code Playgroud)
要回应每个人的状态,请尝试以下方法:
<?php
$string = file_get_contents("/home/michael/test.json");
if ($string === false) {
// deal with error...
}
$json_a = json_decode($string, true);
if ($json_a === null) {
// deal with error...
}
foreach ($json_a as $person_name => $person_a) {
echo $person_a['status'];
}
?>
Run Code Online (Sandbox Code Playgroud)
swi*_*ift 46
最优雅的解决方案:
$shipments = json_decode(file_get_contents("shipments.js"), true);
print_r($shipments);
Run Code Online (Sandbox Code Playgroud)
请记住,json文件必须以UTF-8编码而不使用BOM.如果文件有BOM,则json_decode将返回NULL.
或者:
$shipments = json_encode(json_decode(file_get_contents("shipments.js"), true));
echo $shipments;
Run Code Online (Sandbox Code Playgroud)
Tha*_*ama 17
尝试
<?php
$string = file_get_contents("/home/michael/test.json");
$json_a=json_decode($string,true);
foreach ($json_a as $key => $value){
echo $key . ':' . $value;
}
?>
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 16
没有人指出你开始的"标签"是错误的,完全超出我的范围.您正在使用{}创建对象,而可以使用[]创建数组.
[ // <-- Note that I changed this
{
"name" : "john", // And moved the name here.
"status":"Wait"
},
{
"name" : "Jennifer",
"status":"Active"
},
{
"name" : "James",
"status":"Active",
"age":56,
"count":10,
"progress":0.0029857,
"bad":0
}
] // <-- And this.
Run Code Online (Sandbox Code Playgroud)
通过此更改,json将被解析为数组而不是对象.使用该数组,您可以执行任何操作,例如循环等.
小智 16
试试这个
Run Code Online (Sandbox Code Playgroud)$json_data = '{ "John": { "status":"Wait" }, "Jennifer": { "status":"Active" }, "James": { "status":"Active", "age":56, "count":10, "progress":0.0029857, "bad":0 } }'; $decode_data = json_decode($json_data); foreach($decode_data as $key=>$value){ print_r($value); }
尝试:
$string = file_get_contents("/home/michael/test.json");
$json = json_decode($string, true);
foreach ($json as $key => $value) {
if (!is_array($value)) {
echo $key . '=>' . $value . '<br />';
} else {
foreach ($value as $key => $val) {
echo $key . '=>' . $val . '<br />';
}
}
}
Run Code Online (Sandbox Code Playgroud)
更标准的答案:
$jsondata = file_get_contents(PATH_TO_JSON_FILE."/jsonfile.json");
$array = json_decode($jsondata,true);
foreach($array as $k=>$val):
echo '<b>Name: '.$k.'</b></br>';
$keys = array_keys($val);
foreach($keys as $key):
echo ' '.ucfirst($key).' = '.$val[$key].'</br>';
endforeach;
endforeach;
Run Code Online (Sandbox Code Playgroud)
输出是:
Name: John
Status = Wait
Name: Jennifer
Status = Active
Name: James
Status = Active
Age = 56
Count = 10
Progress = 0.0029857
Bad = 0
Run Code Online (Sandbox Code Playgroud)
循环遍历JSON,foreach
循环为键值对.进行类型检查以确定是否需要进行更多循环.
foreach($json_a as $key => $value) {
echo $key;
if (gettype($value) == "object") {
foreach ($value as $key => $value) {
# and so on
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
<?php
$json = '{
"response": {
"data": [{"identifier": "Be Soft Drinker, Inc.", "entityName": "BusinessPartner"}],
"status": 0,
"totalRows": 83,
"startRow": 0,
"endRow": 82
}
}';
$json = json_decode($json, true);
//echo '<pre>'; print_r($json); exit;
echo $json['response']['data'][0]['identifier'];
$json['response']['data'][0]['entityName']
echo $json['response']['status'];
echo $json['response']['totalRows'];
echo $json['response']['startRow'];
echo $json['response']['endRow'];
?>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
721297 次 |
最近记录: |