在php中用多个对象解码json数组
我无法解码包含多个对象的JSON数组。任何形式的帮助都将被充分利用在php中用多个对象解码json数组,php,json,Php,Json,我无法解码包含多个对象的JSON数组。任何形式的帮助都将被充分利用 { "article_details":[ { "article_code ":"000000000300028156 ", "diff_amnt ":"1 " }, { "article_code ":"000000000300028174 ", "diff_amnt ":"1 "
{
"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 "
}
]
}
使用PHP函数
使用PHP函数
json\u decode
的结果是一个具有两个属性的对象,article\u details
和article
它们中的每一个都是一个对象数组 但是内部对象属性有一个尾随空格,
“article\u code”
和“diff\u 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;
}
还要注意,这些值后面有一个空格。要么修复它的来源,要么像这样过滤
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;
}
json\u decode
的结果是一个具有两个属性的对象,article\u details
和article
它们中的每一个都是一个对象数组 但是内部对象属性有一个尾随空格,
“article\u code”
和“diff\u 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;
}
还要注意,这些值后面有一个空格。要么修复它的来源,要么像这样过滤
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;
}
尝试一下:
$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
尝试一下:
$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
用于解码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 />";
}
因此,您的代码应该如下所示:
$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 />";
}
用于解码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 />";
}
因此,您的代码应该如下所示:
$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 />";
}
$arr=json\u decode($yourray,true)代码>我已经通过了上面的链接。但是代码本身无法工作!!答案可以在本文中找到:)参考资料:实际上我有这些值,但我想知道如何分别选择文章的值<代码>$arr=json\U解码($yourArray,true)代码>我已经通过了上面的链接。但是代码本身无法工作!!答案可以在本文中找到:)参考资料:实际上我有这些值,但我想知道如何分别选择文章的值。。。!!