Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery遍历json数据对象_Jquery_Xml_Json - Fatal编程技术网

如何使用jQuery遍历json数据对象

如何使用jQuery遍历json数据对象,jquery,xml,json,Jquery,Xml,Json,从ajax响应中,我得到一个对象,如下所示-- 你能告诉我怎么才能拿到那张票吗http://example.com/test*"? 我可以循环“内容”、“标题”,但是http://example.com/test*“因为没有与关联的名称 谢谢 这些是问题的关键。。试试这个 var data = obj["highlighting"] for( key in data ){ alert(key) } ​ 这是因为该字符串是键,而不是值。当您迭代对象时,提供一个索引,您就可以获取它。这

从ajax响应中,我得到一个对象,如下所示--

你能告诉我怎么才能拿到那张票吗http://example.com/test*"? 我可以循环“内容”、“标题”,但是http://example.com/test*“因为没有与关联的名称


谢谢

这些是问题的关键。。试试这个

var data = obj["highlighting"]
for( key in data ){

    alert(key)
}
​


这是因为该字符串是键,而不是值。当您迭代对象时,提供一个索引,您就可以获取它。这实际上是第一个密钥。您可能会因此而烧毁我,但是如果您能够的话,您应该考虑重构您的响应。将url与它自己的键放在对象中。这将大大简化对象的导航。
var data = obj["highlighting"]
for( key in data ){

    alert(key)
}
​
var obj = {"highlighting":{//first key
                           "http://example.com/test1": { //first key in first key
                               "content":["sample content 1"],
                               "title"  :["sample title1"]
                                                       },
                           "http://example.com/test2":{ //second key in first key
                               "content":["sample content2"],
                               "title":["sample title1"]
                                                      }
                          }
          }

var first;
for (var i in obj.highlighting) {
    first = obj[i];
    break;
}