1

给定以下从 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}]
          }}]}}}
4

2 回答 2

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)       
}​
于 2012-10-18T00:58:30.977 回答
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);
        });
    });
于 2012-10-18T01:05:04.703 回答