在data[] php中返回JSON


Return JSON inside data[] php

我正在使用以下代码从facebook中提取一些数据:

$tagData = file_get_contents('https://graph.facebook.com/123456789/friends?access_token='.$access_token);
echo $tagData;

这会产生例如:

{"data":
[
{"name":"Jonathan Montiel","id":"28125695"},
{"name":"Jackson C. Gomes","id":"51300292"}
],
"paging":{"next":"https:'/'/graph.facebook.com'/123456789'/friends?access_token=5148835fe&limit=5000&offset=5000&__after_id=100060104"}}

问题我如何才能只返回[...]内部的内容,包括[ ] ?

预期的结果:

[
{"name":"Jonathan James","id":"28125695"},
{"name":"Jackson C. Cliveden","id":"51300292"}
]

试试这个:

$tagData = json_decode( $tagData, true );
$data = $tagData['data'];
echo json_encode( $data );

这基本上将JSON转换为数组,提取所需的部分,并再次以JSON编码的形式返回。

编辑

例子小提琴

json_decodejson_encode重新编码是响应的必要部分。下面的方法对你有用:

$tagData = file_get_contents('https://graph.facebook.com/123456789/friends?access_token='.$access_token);
$tagData = json_decode($tagData);
echo json_encode($tagData->data);

感谢Sirco的灵感,虽然它是如何不同的回答给我没有线索!

$tagData = json_decode(file_get_contents('https://graph.facebook.com/123456789/friends?access_token='.$access_token), true );
$data = $tagData['data'];
echo json_encode( $data );