0
{responseHeader={status=0,QTime=0,params={q=ketan,wt=[javabin,javabin],version=2.2}},response={numFound=1,start=0,docs=[SolrDocument[{user_first_name=Ketan, customerid=5,killname=Hockey, areaname=Paud, categoryname=Sports, last_name=Kore, statename=Maharashtra, cityname=Pune, contactno=124555698}]]}}

我已收到此响应 SOLR 并希望从中获取 JSONObject SOlr 文档。请帮助我如何解析此响应。

4

3 回答 3

0

您提到的字符串是一个 json 字符串(json 是 Javascript Object Notation)。尝试使用 java-json lib 对其进行解码。这是一个例子

于 2012-05-29T13:06:37.580 回答
0

我发现这篇文章很有用。虽然问这个问题的时候已经很成熟了,但它也可能对其他人有所帮助,因为我遇到了同样的问题。我使用 Google API for JSON - GSON 解决了​​这个问题。

Solr 给出的响应不是 JSON 格式。我们需要转换它。我从 solr 检索响应并将其转换为 json 对象。

最初,我的回应是:

   {docs=[SolrDocument{id=1, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Pack, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=1.0.0, packDate=May 22, 2014, 3:10 pm, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1468794552324194304}, SolrDocument{id=2, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Pack New, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=2.0.0, packDate=May 22, 2014, 3:13 pm, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1468794760693022720}, SolrDocument{id=3, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Packs updated, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=3.0.0, packDate=May 22, 2014, 3:32 pm, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1468795898385399808}, SolrDocument{id=5, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Pack New, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=2.0.0, packDate=June 17, 2014, 7:02 am, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1471140110510587904}]}

我的代码:

 HttpSolrServer server= new HttpSolrServer("http://localhost:8983/solr");
        server.setParser(new XMLResponseParser());

        Gson gson= new Gson();

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");

        solrQuery.setHighlight(true);

        solrQuery.setHighlightRequireFieldMatch(true);
        QueryResponse response= server.query(solrQuery);
        SolrDocumentList docs = response.getResults();

        String res= gson.toJson(docs);
        System.out.println(res);

现在,它被转换为 JSON,如下所示:

[
  {
    "id": "1",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Pack",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "1.0.0",
    "packDate": "May 22, 2014, 3:10 pm",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1468794552324194300
  },
  {
    "id": "2",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Pack New",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "2.0.0",
    "packDate": "May 22, 2014, 3:13 pm",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1468794760693022700
  },
  {
    "id": "3",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Packs updated",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "3.0.0",
    "packDate": "May 22, 2014, 3:32 pm",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1468795898385399800
  },
  {
    "id": "5",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Pack New",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "2.0.0",
    "packDate": "June 17, 2014, 7:02 am",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1471140110510588000
  }
]

希望能帮助到你。

于 2014-06-18T09:29:34.007 回答
0

您需要使用哈希映射来制作 JSONObject ,solrj 目前不支持 json 格式作为输出

于 2012-12-20T10:22:42.933 回答