开发者

php How to do a json decode when a child nod is a radom number?

here is a json tree from wikipedia. http://en.wikipedia.org/w/api.php?action=query&titles=japan&prop=categories&format=json

I met a trouble in "pages": { "15573": {. If I will turn the query word, the page number is always changed. How to do a json decode when a child nod is a radom number? Thanks.

{
    "query": {
        "normalized": [
            {
                "from": "japan",
                "to": "Japan"
            }
        ],
        "pages": {
            "15573": {
                "pageid": 15573,
                "ns": 0,
                "title": "Japan",
                "categories": [
                    {
                        "ns": 14,
                        "title": "Category:All articles containing potentially dated statements"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Article Feedback Pilot"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Articles containing Japanese language text"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Articles containing potentially dated statements from 2010"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Articles containing potentially dated statements from January 2011"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Constitutional monarchies"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Countries bordering the Pacific Ocean"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Countries bordering the Philippine Sea"
                    },
                    {
                        "ns": 14,
                        "title": "Category:East Asian countries"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Empires"
                    }
                ]
            }
        }
    },
    "query-con开发者_运维技巧tinue": {
        "categories": {
            "clcontinue": "15573|Featured articles"
        }
    }
}


You can do this (based on what you want in comment):

$json_array = json_decode($json, true);

foreach($json_array['query']['pages'] as $page)
{
    print_r($page['categories']);
}

I'm assuming you want to access it as array, but you can also do it with the default return value, with little modification of course.


After you decode the json, instead of

$arr["15573"]

access the element with

$arr[0]


Use the index instead of the array key to acces the value. example:

$array = json_decode( $json_string );
echo $array['query']['pages'][0]['pageid'];


I guess your problem is not decoding but accessing that node, because you don't know the value? That could be obtained via

$decoded = json_decode( $json, true );
$key     = array_shift( array_keys( $decoded[ 'query' ][ 'pages' ] ) ) );


  1. Use json_decode($json, true); to transform that string to array
  2. Use array_values to convert keys to indexes starting from 0 : $pages = array_values($jsondecoded["query"]["pages"]);

Here is the code and output for you: http://codepad.org/3Usm47YZ

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜