我发现这篇文章很有用。虽然问这个问题的时候已经很成熟了,但它也可能对其他人有所帮助,因为我遇到了同样的问题。我使用 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
}
]
希望能帮助到你。