【问题标题】:How to get the value from nested json object of the given JSON below如何从下面给定 JSON 的嵌套 json 对象中获取值
【发布时间】:2018-04-10 19:27:16
【问题描述】:
{
  "docs":{
    "count":2,
    "users":[
      {
        "_id":"5acc820965c9633fbd851ff7",
        "title":"Yamaha Fazer",
        "description":"this is the dummy description for the yamaha bike. ",
        "image_url":"http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_52c470899a2e1_1.JPG"
      },
      {
        "_id":"5acc8283aa63b6401ac290f0",
        "title":"Apache RR 310",
        "description":"this is the dummy description for the Apache RR 310 bike. ",
        "image_url":"http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_52c470899a2e1_1.JPG"
      }
    ]
  }
}

【问题讨论】:

  • 你需要得到什么价值?你的预期输出是什么?
  • 计数和用户的价值..我在做类似的事情时遇到错误。 docs.count
  • var data = <your JSON object here>; var count = data.docs.count;

标签: json node.js


【解决方案1】:

应该是这样的

d = myObj.docs.count;

u = myObj.docs.users;

【讨论】:

  • 我做过类似 var myObj=data;变量计数=myObj.docs.count;变量计数=myObj.docs.count; ^ 它给出一个错误 TypeError: Cannot read property 'count' of undefined
  • 你能展示你写的完整代码吗?
  • 访问计数:count = myObj.docs.count;获取用户对象的标题:title = myObj.docs.users[0]["title"];这是经过测试并且工作正常。
  • 如果解决成功请标记为答案。
【解决方案2】:
try this,Its Working Fine.

 var demo = data.docs;
            alert(demo.count); ///Count
                var User = demo.users;  ///users
                for (j = 0; User.length > j; j++) {
                    alert(User[j]._id);
                    alert(User[j].title);
                    alert(User[j].description);
                    alert(User[j].image_url);
            }

【讨论】:

  • 您能否参考下一个答案@VarunSingh。
【解决方案3】:
    json:
     var data = {
                    "docs": {
                        "count": 2,
                        "users": [
                          {
                              "_id": "5acc820965c9633fbd851ff7",
                              "title": "Yamaha Fazer",
                              "description": "this is the dummy description for the yamaha bike. ",
                              "image_url": "http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_52c470899a2e1_1.JPG"
                          },
                          {
                              "_id": "5acc8283aa63b6401ac290f0",
                              "title": "Apache RR 310",
                              "description": "this is the dummy description for the Apache RR 310 bike. ",
                              "image_url": "http://666a658c624a3c03a6b2-25cda059d975d2f318c03e90bcf17c40.r92.cf1.rackcdn.com/unsplash_52c470899a2e1_1.JPG"
                          }
                        ]
                    }
                }

javascript:
var demo = data.docs;
            alert(demo.count); ///Count
                var User = demo.users;  ///users
                for (j = 0; User.length > j; j++) {
                    alert(User[j]._id);
                    alert(User[j].title);
                    alert(User[j].description);
                    alert(User[j].image_url);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多