-1

我将如何使用 jquery 解析这个 json?我是 json 新手,所以无法做到这一点。我需要从 json 下面解析名称(imran)和追随者名称(ali & noor)。

{"data":[{"data":{"name":"Imran","email":"imran@example.com","phone":"+9221-000000","location":"khi, pk","userid":"1114","date":"2012 年 7 月 7 日上午 5:39","privacy":"0","type":"0","last_updated":"9 月 11 日, 2012, 8:59 am","images_count":0,"component":"profile"},"0":null,"1":null,"2":[{"following":{"frienduserid" :"1353","name":"Haider"}},{"following":{"frienduserid":"1148","name":"Ali"}}],"3":[{"follower": {"userid":"1148","name":"阿里"}},{"follower":{"userid":"1054","name":"noor"}}]}]}

4

2 回答 2

2

一个非常简单的 PHP 示例:

<php?

// Convert JSON to an associative array
$arr = json_decode('{"data":[{"data":{"name":"Imran","email":"imran@example.com","phone":"+9221-000000","location":"khi,pk","userid":"1114","date":"July 7, 2012, 5:39 am","privacy":"0","type":"0","last_updated":"September 11, 2012, 8:59 am","images_count":0,"component":"profile"},"0":null,"1":null,"2":[{"following":{"frienduserid":"1353","name":"Haider"}},{"following":{"frienduserid":"1148","name":"Ali"}}],"3":[{"follower":{"userid":"1148","name":"Ali"}},{"follower":{"userid":"1054","name":"noor"}}]}]}', true);

// Dump the element containing the name
var_dump($arr['data'][0]['data']['name']);

// Loop through followers and dump each follower's name
foreach ($arr['data'][0][3] as $item)
{
    var_dump($item['follower']['name']);
}
?>
于 2012-09-24T10:04:50.897 回答
0

你的 json 结构看起来很乱……也许你应该检查一下。但是我认为您应该能够通过以下方式访问所需的数据:

jsonObject = JSON.Parse(yourJSONString) //This will create your jsonObject
jsonObject.data[0].data.name --> should hold the name
jsonObject.data[0].data["2"][2].follower.name --> should hold de name of the first follower. 
于 2012-09-24T10:43:14.780 回答