开发者

PHP JSON Data Selection

开发者 https://www.devze.com 2023-01-19 18:30 出处:网络
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.

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",
    开发者_JAVA技巧            "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;


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


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


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号