从json获取数组

时间:2016-05-19 11:21:32

标签: jquery arrays json

我试图从下面的JSON访问chats

{"count":41,"count_id":5,"pic":null,"chats":[{"id":13,"user_id":1,"yiinput_id":5,"chat":"i like to say hello"},{"id":14,"user_id":1,"yiinput_id":5,"chat":"another chat here"}]}

试过这样:

console.log( data );
console.log( data[chats] );
console.log(data[0].chats);
console.log(data[0].chats[0]);

2 个答案:

答案 0 :(得分:0)

使用以下内容:

console.log(JSON.parse(data).chats);

答案 1 :(得分:0)

如果它是一个对象,那么您可以使用data.chats来获取该值,如果它是字符串,那么您需要使用 JSON.parse()来解析json字符串将其转换为对象。



var json = '{"count":41,"count_id":5,"pic":null,"chats":[{"id":13,"user_id":1,"yiinput_id":5,"chat":"i like to say hello"},{"id":14,"user_id":1,"yiinput_id":5,"chat":"another chat here"}]}';

var obj = JSON.parse(json);

console.log(obj.chats);