简体   繁体   English

从JSON获取对象

[英]Getting Object From JSON

I am trying to get a specific object from my json response. 我正在尝试从json响应中获取特定对象。 My response looks like this 我的回应看起来像这样

var list = [{
"count": 1,
"next": null,
"previous": null,
"results": [
    {
        "id": 9,
        "title": "11",
        "description": "111",
    }
  ]
   }];
var products = list.results;
console.log(products)

The result in the console logs says that the object is undefined. 控制台日志中的结果表明该对象未定义。 What am I doing wrong here? 我在这里做错了什么?

list不是JSON(因为JSON是字符串),它是一个数组,因此您必须采用第一个元素:

var products = list[0].results;

List is an array use the array index to select the result 列表是一个数组,使用数组索引选择结果

list[0].results;

 var list = [{ "count": 1, "next": null, "previous": null, "results": [ { "id": 9, "title": "11", "description": "111", } ] }]; var products = list[0].results; console.log(products) 

Your JSON was located at 0th index in that array. 您的JSON位于该数组的第0个索引处。 so simply call your array with index instead calling without index. 因此,只需使用索引调用数组,而不调用没有索引的数组即可。

 var list = [{ "count": 1, "next": null, "previous": null, "results": [ { "id": 9, "title": "11", "description": "111", } ] }]; var products = list[0]; console.log(products.results) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM