Java 从弹性搜索的JSON响应生成自定义JSON对象
我从ElasticSearch得到响应,从该响应中我想形成另一个具有有限字段的JSON(如自定义JSONObject) 请查找我从弹性搜索中得到的响应Java 从弹性搜索的JSON响应生成自定义JSON对象,java,json,elasticsearch,Java,Json,elasticsearch,我从ElasticSearch得到响应,从该响应中我想形成另一个具有有限字段的JSON(如自定义JSONObject) 请查找我从弹性搜索中得到的响应 { "took":93, "timed_out":false, "_shards":{ "total":5, "successful":5, "skipped":0, "failed":0 }, "hits":{ "total":1,
{
"took":93,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":1,
"max_score":1.0,
"hits":[
{
"_index":"attachment",
"_type":"doc",
"_id":"87740",
"_score":1.0,
"_source":{
"app_language":"ES",
"filetype":"PB",
"attachment":{
"date":"2006-05-03T15:17:53Z",
"content_type":"application/pdf",
"author":"JJamesN",
"language":"en",
"title":"Microsoft Word - te7000pb.doc",
"content":"European Electronic Controls Catalog ",
"content_length":12267
},
"ext":"pdf",
"fileContent":"JVBERi0xLjQNJeLjz9MNCjQ3ID"
}
}
]
}
}
请查找我的java代码,该代码试图操纵响应JSON,以创建具有有限字段的单独JSON
JSONObject jsonObject = new JSONObject(responseBody);
JSONObject hits = jsonObject.getJSONObject("hits");
JSONArray hitsArray=hits.getJSONArray("hits");
System.out.println("Hits---->"+hitsArray.toString());
从响应JSON中,我只想创建一个具有以下结构和字段的新JSON
{
"app_language":"ES",
"filetype":"PB",
"attachment.content" : "European Electronic Controls Catalog ",
"ext":"pdf",
}
试试这个:
JSONObject jsonBody = new JSONObject();
jsonBody.put("app_language","value");
jsonBody.put("filetype","value");
jsonBody.put("attachment.content","value");
jsonBody.put("ext",ext);
//String requestBody = jsonBody.toString();
只需从响应中提取您的值,并在任何您想要的地方使用它。
希望这有帮助