我该如何解析这个json数组?这似乎是一个对象

时间:2012-02-28 23:25:04

标签: php arrays json

希望你能提供帮助。我搜索过但无济于事。我需要知道如何将这个json数组读入单独的php变量,然后将其添加到MySQL表中: -

stdClass Object
(
    [status] => ok
    [search] => search_data
    [pagination] => Array
        (
            [next] => /search/data/?query=landrover&limit=3
        )

    [data] => Array
        (
            [0] => Array
                (
                    [domain] => fourwheeler.com
                    [description] => Landrover
                    [user] => Array
                        (
                            [username] => jlyon
                            [full_name] => j lyon
                            [image_url] => http://example.jpg
                            [id] => 200902970785661194
                        )
                    [counts] => Array
                        (
                            [datacount] => 4
                            [comments] => 0
                            [likes] => 0
                        )
                    [id] => 200902833346737840
                    [created_at] => 2011-08-30T05:23:45
                )
            [1] => Array
                (
                    [domain] => 4x4forum.co.uk
                    [description] => Defender
                    [user] => Array
                        (
                            [username] => tjeffries
                            [full_name] => T Jeffries
                            [image_url] => http://example.jpg
                            [id] => 200902970785661194
                        )
                    [counts] => Array
                        (
                            [datacount] => 3
                            [comments] => 2
                            [likes] => 34
                        )
                    [id] => 200902833346737840
                    [created_at] => 2011-09-02T05:12:57
                )

            [2] => Array
                (
                    [domain] => tumblr.com
                    [description] => rangerover
                    [user] => Array
                        (
                            [username] => pjackson
                            [full_name] => p jackson
                            [image_url] => http://example.jpg
                            [id] => 200902970785661194
                        )
                    [counts] => Array
                        (
                            [datacount] => 24
                            [comments] => 3
                            [likes] => 9
                        )
                    [id] => 200902810506737091
                    [created_at] => 2011-09-230T05:19:35
                )

        )

    [query] => landrover
    [counts] => Array
        (
            [places] => 3
            [datapoints] => 500
            [user] => 0
        )

    [pages] => 3
)

我试过这样做但没有运气。

$JSON_Data = json_decode($JSON,true);

foreach($json_output['data'] as $key => $val) {

$domain = ($json_output['data'][$key]['domain']);   
echo $domain."<br/>";
}

任何人都可以帮助我今晚可以睡觉而不会撕裂我的头发吗? ; - )

由于

了Jonatan

1 个答案:

答案 0 :(得分:0)

您在问题中复制的内容是一个对象。这是你正在使用json_decode(),因为这将无法正常工作。它已经是对象,你只需要迭代对象(实际上,它是对象中数组的混合)。

我会调整您包含的代码,如下所示:

//assuming $JSON is not an object already. If it is, ignore this line:
$jsonData = json_decode($JSON); 

foreach($jsonData->data as $val) {
   echo $val['domain'] . '<br/>';
}