有如下字符串,要取出_source内容
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 6,
"successful": 6,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 3.014903,
"hits": [
{
"_index": "nature",
"_type": "nature",
"_id": "AV_cYR20wrZoze0w1DB5",
"_score": 3.014903,
"_source": {
"sid": "@18917795678",
"S0001": "男",
"S0002": "中年",
"S0004": "六档",
"S0005": "未知",
"S0031": "无",
"S0064": "HESTENS|海丝腾_普通",
"S0032": "无",
"S0033": "无",
"S0034": "无",
"S0035": "无",
"S0036": "无",
"S0047": "无",
"mutil03001": "无",
"S0085": "无",
"mutil03002": "无",
"mutil03003": "无",
"s0126": "无",
"s0127": "无",
"s0128": "无",
"s0129": "无",
"s0130": "无"
}
}
]
}
}
public class TestJsonNode {
public static void main(String[] args) throws JsonParseException, JsonMappingException,
IOException {
String json = "{\n" + " \"took\": 4,\n" + " \"timed_out\": false,\n"
+ " \"_shards\": {\n" + " \"total\": 6,\n" + " \"successful\": 6,\n"
+ " \"failed\": 0\n" + " },\n" + " \"hits\": {\n" + " \"total\": 1,\n"
+ " \"max_score\": 3.014903,\n" + " \"hits\": [\n" + " {\n"
+ " \"_index\": \"nature\",\n" + " \"_type\": \"nature\",\n"
+ " \"_id\": \"AV_cYR20wrZoze0w1DB5\",\n" + " \"_score\": 3.014903,\n"
+ " \"_source\": {\n" + " \"sid\": \"@18917795678\",\n"
+ " \"S0001\": \"男\",\n" + " \"S0002\": \"中年\",\n"
+ " \"S0004\": \"六档\",\n" + " \"S0005\": \"未知\",\n"
+ " \"S0031\": \"无\",\n" + " \"S0064\": \"HESTENS|海丝腾_普通\",\n"
+ " \"S0032\": \"无\",\n" + " \"S0033\": \"无\",\n"
+ " \"S0034\": \"无\",\n" + " \"S0035\": \"无\",\n"
+ " \"S0036\": \"无\",\n" + " \"S0047\": \"无\", \n"
+ " \"mutil03001\": \"无\",\n" + " \"S0085\": \"无\",\n"
+ " \"mutil03002\": \"无\",\n" + " \"mutil03003\": \"无\",\n"
+ " \"s0126\": \"无\",\n" + " \"s0127\": \"无\",\n"
+ " \"s0128\": \"无\",\n" + " \"s0129\": \"无\",\n"
+ " \"s0130\": \"无\"\n" + " }\n" + " }\n" + " ]\n" + " }\n"
+ "}";
Map<String, String> resMap = new HashMap<>();
JSONArray jsonArray = JSON.parseObject(json).getJSONObject("hits").getJSONArray("hits");
if (CollectionUtils.isEmpty(jsonArray)) {
System.out.println( resMap);
} else {
JSONObject jsonObject = jsonArray.getJSONObject(0).getJSONObject("_source");
Set<String> keys = jsonObject.keySet();
for (String key : keys) {
resMap.put(key, jsonObject.getString(key));
}
System.out.println( resMap);
}