javascript - 无法使用 JQuery(或仅使用 javascript)解析复杂的 JSON 响应

标签 javascript jquery json parsing

给出以下从 Solr 实例中获取 JSON 数据的函数:

var url = "http://myserver:8080/solr/select?indent=on&version=2.2&q=(title:*Hollis* OR sub_title:*Hollis*+OR+creator:*Hollis*+OR+publisher:*Hollis*+OR+format:*Hollis*++OR+lcsh:*Hollis*++OR+loc_call_num_subject:*Hollis*+OR+note:*Hollis*++OR+toc:*Hollis*)AND+Match_Confidence:[.75+TO+*]&start=0&rows=3500&fl=Geocoded_Field,title,id_inst,Match_Confidence,Conjunct_Longitude1,Conjunct_Latitude1,Anchor,note,creator,format,language,pub_location,publisher,score&wt=json&group=true&group.field=title";

$.getJSON(url, function(data){
    console.log("EXAMPLE TYPE:"+data.grouped.title.groups.docs[0].title);
});

例如,我如何循环浏览每个响应“文档”(下面发布的数据)并抓取每个“标题”事件?我已经尝试了 data.grouped.title.groups.docs[0].title 的多种变体,但没有成功。我想我在数据访问字符串 (data.grouped.title.groups.docs.title) 中的顺序不正确。

从浏览器复制和粘贴的查询返回的示例数据:

{   "responseHeader":{
    "status":0,
    "QTime":902,
    "params":{
      "indent":"on",
      "wt":"json",
      "version":"2.2",
      "rows":"3500",  "fl":"Geocoded_Field,title,id_inst,Match_Confidence,Conjunct_Longitude1,Conjunct_Latitude1,Anchor,note,creator,format,language,pub_location,publisher,score",
      "start":"0",
      "q":"(title:*Hollis* OR sub_title:*Hollis* OR creator:*Hollis* OR publisher:*Hollis* OR format:*Hollis*  OR lcsh:*Hollis*  OR loc_call_num_subject:*Hollis* OR note:*Hollis*  OR toc:*Hollis*)AND Match_Confidence:[.75 TO *]",
      "group.field":"title",
      "group":"true"}},   "grouped":{
    "title":{
      "matches":2533,
      "groups":[{
          "groupValue":"Thomas Hollis [and] Thomas Brand Hollis",
          "doclist":{"numFound":3,"start":0,"maxScore":0.75592893,"docs":[
              {
                "title":"Thomas Hollis [and] Thomas Brand Hollis",
                "Match_Confidence":0.894584,
                "Conjunct_Latitude1":41.89,
                "Conjunct_Longitude1":12.5,
                "Geocoded_Field":[
                  "note"],
                "pub_location":[
                  "1752"],
                "Anchor":[
                  "Roma"],
                "id_inst":[
                  "009360446"],
                "language":["English"],
                "format":["Other"],
                "note":[
                  "Two bust portraits carved in bas-relief. One is of Thomas Hollis, the elder (1659-1731), and one of his friend and heir, Thomas Brand Hollis (ca.1719-1804). Portraits are framed together",
                  "Inscribed on front of each sculpture: Andrea Pozzi fece dal naturale; verso of Thomas Hollis inscribed: Ritratto del Sig: re Tommaso / Hollis, Cau: re Inglese, Termina= / to in Roma nel suo Giorno Nat= / alizio de i 14 Aprile 1752 in / Et di 32 Ani; verso of Thomas Brand Hollis inscribed: Ritratto dell'Illust: o Sig: re / Tommaso Brand, Caualiere / Inglese, Fatto in Roma / Nell' Anno 1752",
                  "Title taken from plaques",
                  "Framed and glazed"],
                "creator":["Pozzi, Andrea, 1718-1769","Hollis, Thomas, 1720-1774, former owner"],
                "score":0.75592893}]
          }},
        {
          "groupValue":"The post of duty",
          "doclist":{"numFound":24,"start":0,"maxScore":0.5459487,"docs":[
              {
                "title":"The post of duty",
                "Match_Confidence":0.985783,
                "Conjunct_Latitude1":42.4842,
                "Conjunct_Longitude1":-76.4799,
                "Geocoded_Field":[
                  "lcsh"],
                "pub_location":[
                  "Coxsackie, N. Y"],
                "Anchor":[
                  "Lansing"],
                "id_inst":[
                  "006317718"],
                "language":["English"],
                "format":["Book"],
                "note":[
                  "Published by request"],
                "publisher":[
                  "F.C. Dedrick, Printer"],
                "creator":["Zabriskie, Francis Nicoll, 1832-1891"],
                "score":0.5459487}]
          }},
        {
          "groupValue":"Discourses concerning government: in way of dialogue",
          "doclist":{"numFound":1,"start":0,"maxScore":0.41996053,"docs":[
              {
                "title":"Discourses concerning government: in way of dialogue",
                "Match_Confidence":0.95121,
                "Conjunct_Latitude1":51.5142,
                "Conjunct_Longitude1":-0.093145,
                "Geocoded_Field":[
                  "pub_location"],
                "pub_location":[
                  "London"],
                "Anchor":[
                  "London"],
                "id_inst":[
                  "006199101"],
                "language":["English"],
                "format":["Book"],
                "note":[
                  "First published 1681 under title: Plato redivivus",
                  "Bound in old mottled calf, rebacked"],
                "publisher":[
                  "Printed, and sold by A. Baldwin"],
                "creator":["Neville, Henry, 1620-1694","Hollis, Thomas, 1720-1774, former owner"],
                "score":0.41996053}]
          }}]}}}

最佳答案

查看此 jsfiddle http://jsfiddle.net/ByxHV/

for (var i =0; i < data.grouped.title.groups.length; i++) {
    var  group = data.grouped.title.groups[i];
    console.log(group.groupValue, group.doclist.numFound)       
}​

关于javascript - 无法使用 JQuery(或仅使用 javascript)解析复杂的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12945595/

相关文章:

javascript - 按键按下的时间长度

javascript - 当我点击它时,它是最后一个可能的值

javascript - JSON 访问并在 Web 浏览器中显示

java - 嵌套数组的GSON解析

json - golang 从 json 字符串中获取 key 的有效方法/lib,并使用此 key 执行 geoip,然后将 geoip 信息添加到 json

javascript - Google Apps 脚本将已完成的项目移动到另一个电子表格

javascript - 正则表达式 Hodorize 文本

javascript - 从 HTML 字符串中获取 JSON 对象

javascript - jQuery load() 在 IE 中抛出 "permission denied"错误

javascript - 滚动动画不适用于元素