我有以下JSON字符串:
{"Data":{"Recipes":{"Recipe_5":{"ID":"5","TITLE":"Spaghetti Bolognese"},"Recipe_7":{"ID":"7","TITLE":"Wurstel"},"Recipe_9":{"ID":"9","TITLE":"Schnitzel"},"Recipe_10":{"ID":"10","TITLE":null},"Recipe_19":{"ID":"19","TITLE":null},"Recipe_20":{"ID":"20","TITLE":"Hundefutter"},"Recipe_26":{"ID":"26","TITLE":"Apfelstrudel"},"Recipe_37":{"ID":"37","TITLE":null},"Recipe_38":{"ID":"38","TITLE":"AENDERUNG"},"Recipe_39":{"ID":"39","TITLE":null},"Recipe_40":{"ID":"40","TITLE":"Schnitzel"},"Recipe_42":{"ID":"42","TITLE":"Release-Test"},"Recipe_43":{"ID":"43","TITLE":"Wurstel2"}},"recipes_id":{"ranking_1":"9","ranking_2":"10","ranking_3":"7","ranking_4":"5"}},"Message":null,"Code":200}
如何在PHP中解析它并提取TITLEs 列表?
TITLE
您可以使用该函数json_decode来解析PHP中的JSON数据(至少=> 5.2.0)。一旦有了PHP对象,就可以轻松地遍历所有配方/成员并使用以下内容访问其标题:
json_decode
$data = json_decode($json, true); // yields associative arrays instead of objects foreach ($data['Data']['Recipes'] as $key => $recipe) { echo $recipe['TITLE']; }
(对不起,我现在无法真正运行此代码。希望无论如何它都会有所帮助。)