如何使用php获取json编码数据

时间:2018-01-01 08:33:53

标签: php json

echo $someJSON=json_encode($resultData);

输出为{"status":true,"postData":[{"post_id":"3","post_title":"JAVA","post_desc":"JAVA DESCRIPTION","status":"1"},{"post_id":"1","post_title":"PHP API","post_desc":"MAKING PHP API","status":"1"}]}

从输出中,如何使用PHP获取'post_title'的值?

3 个答案:

答案 0 :(得分:0)

$arr = json_decode('{"status":true,"postData":[{"post_id":"3","post_title":"JAVA","post_desc":"JAVA DESCRIPTION","status":"1"},{"post_id":"1","post_title":"PHP API","post_desc":"MAKING PHP API","status":"1"}]}');

foreach($arr->postData as $post) {
    $title = $post->post_title;
}

答案 1 :(得分:0)

$resultData已经是一个对象,因此您无需转换它。 只需像任何其他对象一样访问其属性。 例如:

foreach($resultData->postData as $data) {
    echo $data->post_title, ', ';
}

演示:http://rextester.com/JHFH10035

答案 2 :(得分:0)

您必须先使用json_decode

对其进行解码
$someJson = json_decode('{"status":true,"postData":[{"post_id":"3","post_title":"JAVA","post_desc":"JAVA DESCRIPTION","status":"1"},{"post_id":"1","post_title":"PHP API","post_desc":"MAKING PHP API","status":"1"}]}');

foreach ($someJson->postData as $data){
    echo $data->post_title;
}