解析JSON在PHP不工作
问题描述:
{
"data": [
{
"business_type": "bar",
"business_name": "KKK",
"number": "1234567",
"business_id": "test1"
},
{
"business_type": "restauratns",
"business_name": "KKKK",
"number": "1234567",
"business_id": "test2"
}
]
}
PHP代码: -解析JSON在PHP不工作
1)是否加工
foreach ($json_array->data as $obj) {
echo $obj->business_name;
echo $obj->business_type;
echo $obj->business_id;
}
2)仅得到输出的 “数据阵”,但没有获得对象的详细信息
foreach ($json_array as $name=>$value) {
echo "$name: $value\n";
foreach ($value as $entry) {
echo ' ' . $entry->business_type;
echo ' ' . $entry->business_name;
echo ' ' . $entry->business_id;
echo ' ' . $entry->number;
}
}
之后我也试着用print_r和var_dump来打印细节。
结果的var_dump是: -
array(1) {
["data"]=>
array(2) {
[0]=>
array(4) {
["business_type"]=>
string(3) "bar"
["business_name"]=>
string(3) "KKK"
["number"]=>
string(7) "1234567"
["business_id"]=>
string(5) "test1"
}
[1]=>
array(4) {
["business_type"]=>
string(11) "restauratns"
["business_name"]=>
string(4) "KKKK"
["number"]=>
string(7) "1234567"
["business_id"]=>
string(5) "test2"
}
}
}
我想找到我在哪里犯错,但没有得到任何想法。如果有人有任何想法,请引导我。
答
你的JSON数据很显然解码为阵列,不为对象。 json_decode(..., true)
返回数组,如果省略参数true
它返回对象。要么更改您的代码以期望数组或更改解码以返回对象。
答
array.php
<?php
// $json = json_decode($json_data, true);
$json = array(
'data' => array(
array(
'bussiness_type' => 'bar',
'bussiness_name' => 'KKK',
'number' => '1234567',
'bussiness_id' => 'test1'
),
array(
'bussiness_type' => 'restauratns',
'bussiness_name' => 'KKKK',
'number' => '1234567',
'bussiness_id' => 'test2'
)
)
);
/*
array(1) {
["data"]=>
array(2) {
[0]=>
array(4) {
["bussiness_type"]=>
string(3) "bar"
["bussiness_name"]=>
string(3) "KKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test1"
}
[1]=>
array(4) {
["bussiness_type"]=>
string(11) "restauratns"
["bussiness_name"]=>
string(4) "KKKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test2"
}
}
}
*/
foreach($json['data'] as $value)
{
echo $value['bussiness_type'] . "\n"
. $value['bussiness_name'] . "\n"
. $value['number'] . "\n"
. $value['bussiness_id'] . "\n\n";
}
?>
object.php
<?php
// $json = json_decode($json_data, true);
$json = array(
'data' => array(
array(
'bussiness_type' => 'bar',
'bussiness_name' => 'KKK',
'number' => '1234567',
'bussiness_id' => 'test1'
),
array(
'bussiness_type' => 'restauratns',
'bussiness_name' => 'KKKK',
'number' => '1234567',
'bussiness_id' => 'test2'
)
)
);
$to_json = json_encode($json);
$to_obj = json_decode($to_json);
/*
object(stdClass)#1 (1) {
["data"]=>
array(2) {
[0]=>
object(stdClass)#2 (4) {
["bussiness_type"]=>
string(3) "bar"
["bussiness_name"]=>
string(3) "KKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test1"
}
[1]=>
object(stdClass)#3 (4) {
["bussiness_type"]=>
string(11) "restauratns"
["bussiness_name"]=>
string(4) "KKKK"
["number"]=>
string(7) "1234567"
["bussiness_id"]=>
string(5) "test2"
}
}
}
*/
foreach($to_obj->data as $value)
{
echo $value->bussiness_type . "\n"
. $value->bussiness_name . "\n"
. $value->number . "\n"
. $value->bussiness_id . "\n\n";
}
?>