小编典典

在PHP中解析JSON

json

我有以下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 列表?


阅读 327

收藏
2020-07-27

共1个答案

小编典典

您可以使用该函数json_decode来解析PHP中的JSON数据(至少=> 5.2.0)。一旦有了PHP对象,就可以轻松地遍历所有配方/成员并使用以下内容访问其标题:

$data = json_decode($json, true); // yields associative arrays instead of objects
foreach ($data['Data']['Recipes'] as $key => $recipe) {
    echo $recipe['TITLE'];
}

(对不起,我现在无法真正运行此代码。希望无论如何它都会有所帮助。)

2020-07-27