php当子节点是随机数时,如何执行json_decode?

时间:2011-08-03 09:38:46

标签: php json

这是来自维基百科的json树。 http://en.wikipedia.org/w/api.php?action=query&titles=japan&prop=categories&format=json

我在"pages": { "15573": {遇到了麻烦。如果我将翻转查询字,则页码始终会更改。当孩子点头是一个radom号时,如何进行json解码?谢谢。

{
    "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-continue": {
        "categories": {
            "clcontinue": "15573|Featured articles"
        }
    }
}

5 个答案:

答案 0 :(得分:1)

解码json后,而不是

$arr["15573"]

使用

访问元素
$arr[0]

答案 1 :(得分:1)

使用索引而不是数组键来访问该值。 例如:

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

答案 2 :(得分:1)

你可以这样做(根据你想要的评论):

$json_array = json_decode($json, true);

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

我假设您要将其作为数组访问,但您也可以使用默认返回值进行访问,当然几乎没有修改。

答案 3 :(得分:1)

我猜你的问题不是解码而是访问那个节点,因为你不知道这个值?这可以通过

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

答案 4 :(得分:1)

  1. 使用json_decode($json, true);将该字符串转换为数组
  2. 使用array_values将键转换为从0开始的索引: $pages = array_values($jsondecoded["query"]["pages"]);
  3. 以下是您的代码和输出:http://codepad.org/3Usm47YZ