简体   繁体   English

无法使用JavaScript访问嵌套的json元素

[英]Can't access nested json elements with javascript

I'm really stuck on how to access nested elements of a json response that I'm getting from my api. 我真的迷上了如何访问从我的api获取的json响应的嵌套元素。 I validated the json and it shows as valid. 我验证了json,并显示为有效。

For instance, how do I access the numFound attribute? 例如,如何访问numFound属性? Here's my code that is not working: 这是我的代码不起作用:


$.ajax({
    url: "/api/SearchAPI/infopop?id=" + songID,
    datatype:"json",
    method: "get"
}).done(function (data) {
    var obj = JSON.parse(data);
    alert(obj);                   /* This displays the entire json response 
    alert(obj.response.numFound)    /* This does not work
    alert(data.response.numFound)    /* This does not work

Here's the response I'm trying to access 这是我尝试访问的回复

{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"*:*",
      "indent":"off",
      "fl":"Name,Description,Keywords,ISRC,Instruments,Lyrics,Bpm,Vocal,Tempo,Key,TV_Genres,Music_Genres,length\r\n,Writers,profileImagePath\r\n,Publishers\r\n,songImagePath\r\n,Band_Styles",
      "start":"0",
      "callback":"?\r\n",
      "fq":[
        "id:00106c8c-7e21-4e75-80da-cdff8e6d3d44",
        "publicflag:1",
        "pubname:komposed"
      ],
      "rows":"10",
      "version":"2.2",
      "wt":"json"
    }
  },
  "response":{
    "numFound":1,
    "start":0,
    "docs":[
      {
        "profileImagePath":[
          "https://komposed.blob.core.windows.net/jrock-1873564a-d409-4370-80d8-23dc97114f18/songimage/f557110f-4ad3-4353-a1b3-3ba70d52e8f0?sv=2014-02-14&sr=b&sig=Q3ywrwEa6URp%2FPCvK0Ngesza8PBhMEmE5ONeKhw8vE4%3D&st=2016-06-16T16:49:45Z&se=2066-06-16T16:54:45Z&sp=r&rsct=application%2Foctet-stream&rscd=attachment%3B%20filename%3Dredneck.jpeg"
        ],
        "songImagePath":[
          "https://komposed.blob.core.windows.net/jrock-1873564a-d409-4370-80d8-23dc97114f18/songimage/f0c00dcd-69f9-42af-b236-4a53f0d78e76?sv=2014-02-14&sr=b&sig=oF0STMDddyuJiNZO%2BE78sYtbboC4ic%2Fl4bR5ESBFouE%3D&st=2016-06-16T16:48:59Z&se=2066-06-16T16:53:59Z&sp=r&rsct=application%2Foctet-stream&rscd=attachment%3B%20filename%3DTENSION%20LAST%20MAN.jpg"
        ],
        "Name":[
          "Bottle Service Tension"
        ],
        "Description":[
          "Cool crime scene track with a chill night club vibe"
        ],
        "Bpm":[
          90
        ],
        "Vocal":[
          "Acapella"
        ],
        "Tempo":[
          "Fast"
        ],
        "Writers":[
          "Justin Sirota|100.00|"
        ],
        "Keywords":[
          "club, crime, investigation, cool, lounge, pulse"
        ],
        "TV_Genres":[
          "Tension"
        ]
      }
    ]
  }
}

Just use obj.numFound . 只需使用obj.numFound response is your data argument. response是您的data参数。

$.ajax({
...
}).done(function (response) {
  alert(response.numFound)
...

Also, since you're telling jQuery (assuming this based on the code) that the response is of JSON type via datatype:"json", you don't need to do JSON.parse on it. 另外,由于您通过datatype:"json",告诉jQuery(假设基于代码),响应为JSON类型datatype:"json",您无需对其执行JSON.parse

Turns out the issue was in my API. 原来问题出在我的API中。 I was returning json as a string instead of an object. 我将json作为字符串而不是对象返回。 Once I did that, javascript was able to reference the json elements. 一旦这样做,javascript就可以引用json元素。 Thanks for trying to help! 感谢您的帮助!

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

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