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

时间:2012-10-18 00:52:01

标签: 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);
});

如何循环浏览每个响应'doc'(下面发布的数据)并抓住每个“标题”事件?我尝试了很多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}]
          }}]}}}

2 个答案:

答案 0 :(得分:2)

请参阅此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)       
}​

答案 1 :(得分:0)

我使用JSON Pretty Print来检查JSON

var titleList = [];

    $.each(data.grouped.title.groups, function(index, value) {
        $.each(value.doclist.docs, function(index, value) {
            titleList.push(value.title);
        });
    });