我无法解码具有多个对象的JSON数组。任何形式的帮助都将被充分使用。
{
"article_details":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
],
"article":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
]
}
答案 0 :(得分:1)
尝试一下:
$json = '
{
"article_details":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
],
"article":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
]
}';
$key = 'article_code '; // Since your key is 'article_code ' but not 'article_code', I have to do this
$dataObject = json_decode($json);
echo $dataObject->article_details[0]->$key; // This will out put: 000000000300028156
$dataArray = json_decode($json, true);
echo $dataArray['article_details'][0]['article_code ']; // This will out put: 000000000300028156
答案 1 :(得分:1)
使用json_decode()
解码JSON字符串,如下所示:
$json_array = json_decode($json, true);
^ When TRUE, returned objects will be converted into associative arrays.
所以你的代码应该是这样的:
// Here $json is your json string
$json_array = json_decode($json, true);
foreach($json_array as $key => $arrays){
echo $key . "<br />";
foreach($arrays as $array){
foreach($array as $key => $value){
echo $key . " => " . $value . "<br />";
}
}
echo "<br />";
}
输出:
article_details
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1
article
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1
答案 2 :(得分:0)
使用PHP json_decode功能
<?php
$str = '{
"article_details":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
],
"article":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
]
}';
var_dump(json_decode($str,true));
输出:
array(2) {
["article_details"]=>
array(3) {
[0]=>
array(2) {
["article_code "]=>
string(19) "000000000300028156 "
["diff_amnt "]=>
string(2) "1 "
}
[1]=>
array(2) {
["article_code "]=>
string(19) "000000000300028174 "
["diff_amnt "]=>
string(2) "1 "
}
[2]=>
array(2) {
["article_code "]=>
string(19) "000000000300028126 "
["diff_amnt "]=>
string(2) "1 "
}
}
["article"]=>
array(3) {
[0]=>
array(2) {
["article_code "]=>
string(19) "000000000300028156 "
["diff_amnt "]=>
string(2) "1 "
}
[1]=>
array(2) {
["article_code "]=>
string(19) "000000000300028174 "
["diff_amnt "]=>
string(2) "1 "
}
[2]=>
array(2) {
["article_code "]=>
string(19) "000000000300028126 "
["diff_amnt "]=>
string(2) "1 "
}
}
}
答案 3 :(得分:0)
json_decode
的结果是一个包含两个属性article_details
和article
的对象
每个都是一个对象数组。
但内部对象属性有一个尾随空格"article_code "
和"diff_amnt "
您可以按如下方式迭代它们
$object = json_decode($str);
foreach($object->article_details as $articleDetails){
print $articleDetails->{"article_code "} . PHP_EOL;
}
foreach($object->article_details as $article){
print $article->{"diff_amnt "} . PHP_EOL;
}
另请注意,值具有尾随空格
要么修复它的来源,要么像{/ p>那样过滤json
字符串中的尾随空格
$jsonString = str_replace(' "','"',$jsonString);
在双引号前明确删除任何尾随空格 然后以通常的方式访问对象属性
foreach($object->article_details as $articleDetails){
print $articleDetails->article_code . PHP_EOL;
}
foreach($object->article_details as $article){
print $article->diff_amnt . PHP_EOL;
}