json字符串转换

有如下字符串,要取出_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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值