0

I´ve got the following JSON string:

{"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}

How can I parse it in PHP and extract a list of TITLEs?

2
  • 2
    In addition to not specifying in what language / with what tools you want to parse this JSON object: Where does the Spaghetti Bolognese in the end come from? This makes it malformed JSON. Commented Dec 15, 2011 at 16:04
  • Sorry, forget about that. Fore sure, it´s not a part of the JSON string. Commented Dec 15, 2011 at 16:09

2 Answers 2

3

You can use the function json_decode to parse JSON data in PHP (>= 5.2.0, at least). Once you have a PHP object, it should be easy to iterate over all recipes/members and access their titles, using something like this:

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

(Sorry I can't actually run this code right now. Hope it helps anyway.)

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I already have that! $obj = json_decode($jsonstring); Now, In my $obj is the JSON I posted above. But how can i iterate over the recipes? Sorry, I searched so much already but haven´t found it anywhere
Using associative arrays and foreach should do the trick—see my updated answer.
Wow great! My problem was that I didn´t have the "true" in the decode method. Why do I need that?
As stated in the comment, the true results in an associative array being returned, as opposed to a plain PHP object. Associative arrays are easier being traversed, using foreach as you can see.
0

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);

4 Comments

Ok, I understand that. But what is, if I want the TITLE of each Recipe in the JSON string, so that I not have to know that Recipe_5 exists.
I print it out with php... print_r($obj->Data->Recipes->Recipe_5->TITLE); for example gives me the title of recipe5, but I want to have all the Titles in Recipes
Again, what do you want to do with these titles? I edited my answer to explain how to put all of them in a JavaScript array—don't know if that is what you want though.
I edited your question so that it reflects you're interested in PHP and added a new, short answer. (I'm not sure whether this is the preferred way to handle this situation... is it, stackoverflow pros?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.