1

How would I go about selecting the data of each title from the following JSON? I have the JSON decoded, but I'm not sure how to select the part I want.

{
    "responseData": {
        "results": [
            {
                "title": "Justin Giesbrecht 749",
                "titleNoFormatting": "Justin Giesbrecht 749",
            },
            {
                "title": "Gopher dunes 09",
                "titleNoFormatting": "Gopher dunes 09",
            },
            {
                "title": "dirtbike Justin",
                "titleNoFormatting": "dirtbike Justin",
            },
            {
                "title": "A Warming",
                "titleNoFormatting": "A Warming",
            }
        ],
        "cursor": {
            "pages": [
                {
                    "start": "0",
                    "label": 1
                },
                {
                    "start": "4",
                    "label": 2
                }
            ],
            "estimatedResultCount": "6",
            "currentPageIndex": 0,
        }
    },
    "responseDetails": null,
    "responseStatus": 200
}

I thought it would be something like this, but I don't get anything:

echo "Response ". $jsonS->responseData->results[1]->title;
2
  • When you say you don't get anything, what do you mean? Do you see "Response" or not? Have you tried var_dump($jsonS);? Commented Oct 14, 2010 at 0:36
  • Sorry, yes I do see response, but I see nothing of the jsonS, sorry. Commented Oct 14, 2010 at 15:42

4 Answers 4

5

Actually you've got the reading of the title part right, it's the JSON that is invalid.

Copying the JSON into a JSON validator/lint e.g. http://www.jsonlint.com/ will show that the you have additional , (commas) after the last object attribute in a few places (5 places to be exact, after each 'titleFormatting' attribute and after 'currentPageIndex').

If you fix those errors and parse it using json_decode e.g.:

$jsonS = json_decode($json_text);

Then your own code:

echo "Response " . $jsonS->responseData->results[1]->title;

Will output the second (index 1 being the second index) results title

Response Gopher dunes 09

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

2 Comments

+1: I answered that it was invalid, but your answer actually tells him why. Nice work!
Thank you, it was minor, but you were right! appreciate your time! Cheers. [answered]
0

When you json_decode something it is converted to a regular PHP array, so you can reference it like $decodedObject["nodeName"].

1 Comment

As @Josh mentioned, this only works if you use json_decode($json_data, true)
0

When I parse that JSON as you quoted it with PHP's json_decode method, it gives me NULL because PHP doesn't think it's valid JSON. Add a var_dump($jsonS); to see if this is happening to you as well. If it is, you may need to be sure your JSON is valid.

Here's what I did, for reference:

$json_data = <<<END_OF_JSON
{   
    "responseData": {        "results": [
            {
                "title": "Justin Giesbrecht 749",                "titleNoFormatting": "Justin Giesbrecht 749",
            },
            {
                "title": "Gopher dunes 09",
                "titleNoFormatting": "Gopher dunes 09",
            },
            {
                "title": "dirtbike Justin",
                "titleNoFormatting": "dirtbike Justin",
            },
            {
                "title": "A Warming",
                "titleNoFormatting": "A Warming",
            }
        ],
        "cursor": {
            "pages": [                {   
                    "start": "0",
                    "label": 1
                },
                {   
                    "start": "4",
                    "label": 2
                }
            ],
            "estimatedResultCount": "6",
            "currentPageIndex": 0,
        }
    },
    "responseDetails": null,
    "responseStatus": 200
}
END_OF_JSON;
$jsonS = json_decode($json_data);
var_dump($jsonS);
echo "Response ". $jsonS->responseData->results[1]->title;

And the output was:

NULL Response

If you're using different JSON, please edit your question and share it. But if you're actually using the above, it's not valid...

Comments

0

In JSON you are not allowed to leave trailing comma in array/object definition. So, when in PHP is perfectly valid to write:

$a = array(1,2,3,);

(note last comma), in JSON

a : [1,2,3,] 

or

a : {x :'y',}

is invalid.

Comments

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.