JSON解析多层次

时间:2018-04-24 09:02:49

标签: php

如何从JSON下面获取文字

https://jsfiddle.net/7h6a55m6/2/ JSON数据

需要获得“测试”,“测试2”等。坚持多级JSON风格。什么是最简单的方法

foreach ($response->data as $res) {
  echo $res['comments'];

}

2 个答案:

答案 0 :(得分:1)

使用json_decode()。如下所示

$test_json='
{ "media": 
        { "data": 
            [ 
                { "comments": 
                    { "data": 
                        [ 
                            { "text": "Testing", "id": "17935572247064063" }, 
                            { "text": "Testing2", "id": "17909467621160083" }, 
                            { "text": "Testing3", "id": "17879193508206704" }, 
                            { "text": "Testing4", "id": "17936230114007492" }, 
                            { "text": "Testing5", "id": "17861359981236880" }, 
                            { "text": "Testing6", "id": "17932586956016890" }, 
                            { "text": "Testing7", "id": "17920569544116678" }, 
                            { "text": "Testing8", "id": "17933592700059204" } 
                        ] 
                    } 
                } 
            ] 
        } 
    }
';
$test_json=json_decode($test_json,true);


foreach($test_json['media']['data'][0]['comments']['data'] as $row){
    print_r($row['text']);

}

答案 1 :(得分:-1)

尝试

$responseData = json_decode($response->data, true);
$result = $responseData['media']['data'][0]['comments']['data'];
foreach($result as $data) {
    // do your stuff here
}