Parsing JSON in PHP

前端 未结 2 1331
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 10:27

I´ve got the following JSON string:

{\"Data\":{\"Recipes\":{\"Recipe_5\":{\"ID\":\"5\",\"TITLE\":\"Spaghetti Bolognese\"},\"Recipe_7\":{\"ID\":\"7\",\"TITLE         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-22 11:14

    If you want to do that in JavaScript, you can simply access JSON data like "normal" objects:

    var jsonData = {
        "Data": {"Recipes": {"Recipe_5": {"ID":"5","TITLE":"Spaghetti Bolognese"}}}
        // more data
    };
    alert(jsonData.Data.Recipes.Recipe_5.TITLE);
    

    This will print the TITLE of Recipe_5 in a message box.

    EDIT:

    If you want all the titles in a list, you can do something like this:

    var titles = [];
    for (var key in jsonData.Data.Recipes) {
        var recipe = jsonData.Data.Recipes[key];
        titles.push(recipe.TITLE);
    }
    alert(titles);
    

提交回复
热议问题