解码嵌套的JSON

时间:2011-11-25 11:21:56

标签: json

我试图解码我的嵌套JSON:

{"Data":{"Recipes":{"Recipe_7":{"ID":"7","TITLE":"Wurstel","TEXT":"Kochen","COUNT_PERSONS":"4","DURATION":"10","USER_ID":"1","DATE":"2011-09-09 18:38:20"}}},"Message":null,"Code":200}

以下内容:

include('php/get_recipe_byID.php');
$jsonstring = $a;
echo $jsonstring;
$obj = json_decode($jsonstring);
print_r($obj->Data);
print_r($obj->data[0]->Recipe_7->title);

的print_r($ obj->数据);回声

stdClass Object ( [Recipes] => stdClass Object ( [Recipe_7] => stdClass Object ( [ID] => 7 [TITLE] => Wurstel [TEXT] => Kochen [COUNT_PERSONS] => 4 [DURATION] => 10 [USER_ID] => 1 [DATE] => 2011-09-09 18:38:20 ) ) ) 

的print_r($ obj->数据[0] - > Recipe_7->标题);回声

Notice: Undefined property: stdClass::$data in /var/www/recipe_search.php on line 126

Notice: Trying to get property of non-object in /var/www/recipe_search.php on line 126

我认为我的语法错了,不是吗?

1 个答案:

答案 0 :(得分:0)

$obj->Data,对吗?

PHP变量区分大小写。 SO $obj->data和$ obj->数据

编辑:

将索引存储在变量中以动态访问对象值,如下所示

$recipe_index = 'Recipe_'. $_GET['id']. print_r($obj->Data->Recipes->$recipe_index]->TITLE);