从Java中的复杂JSON提取特定值 [英] Extract specific value from complex JSON in java

查看:110
本文介绍了从Java中的复杂JSON提取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的复杂json,我需要解析此json,并在Java中从该复杂json打印仅ak,dt和mi的值...希望您能对我有所帮助...

I have got a valid complex json and I need to parse this json and print the values of only ak, dt and mi from this complex json in java... hope you can help me...

{
  "CP": "{\"e\":{\"h\":{\"ak\":\"1c8d1d7eaa32ff3f58a882\",\"at\":\"app\"},\"c\":{\"dt\":\"MEmulator\",\"mi\":\"DD278047D56BF292F1FC16F\",\"ui\":\"m4J\/2s=\",\"av\":\"0.2\",\"pn\":\"WP\",\"pv\":\"7.10\",\"nv\":\"C# 1.1.0\",\"al\":\"en\"},\"b\":[{\"ts\":139658547,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":8,\"ev\":\"sessionStart\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{\"DaySessionStarted\":\"Tuesday\"}},{\"ts\":1319549658751,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":3,\"ev\":\"AutomaticFeedRefresh\",\"si\":\"19477682-de5ec331dc33\",\"tt\":{}},{\"ts\":1319549675609,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549677179,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549678401,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549679973,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-c9-19bec331dc33\",\"tt\":{}}],\"tt\":{\"OSV\":\"ME\"}}}",
  "SP": {
    "httpHeaders": {
      "x-bluecoat-via": [
        "35D3468F4D5F18"
      ],
      "content-type": [
        "application\/x-form-ur"
      ],
      "connection": [
        "Keep-Alive"
      ],
      "host": [
        "20.198.134.198:8080"
      ],
      "accept": [
        "text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2"
      ],
      "content-length": [
        "1791"
      ],
      "user-agent": [
        "Java\/1.6.0_23"
      ]
    },
    "senderIp": [
      "112.101.216.113"
    ],
    "receiveTimeStamp": "2012-06-26T06:29:36+0000"
  }
}

推荐答案

就像其他人建议的那样,那里有很多可以使用的库(npe建议看起来非常不错).另一方面,如果您只有那些简单的案例,而您实际上并不需要对JSON做其他任何事情,那么也许您所需要的只是一个正则表达式.本质上,JSON只是文本,因此您可以执行以下操作:

Like the others have suggested there are numerous libs out there that you can use (npe suggestion seems really nice). On the other hand, if you only have those simple cases and you don't really need to do anything else with JSON, maybe all you need is a regex. In essence JSON is just text, so you can do something like this:

    Pattern akPattern = Pattern.compile("ak\":\"([^\"]+)");
    Matcher matcher = akPattern.matcher(jsonAsString);

    matcher.find();
    String akValue = matcher.group(1);

    System.out.println(akValue);

这会打印出"ak"的值.

This prints out the value for "ak".

但是同样,只有在没有其他JSON要求的情况下,我才会这样做.否则,请使用JSON库.

But again, I would only do this if I didn't have any other JSON requirements. Otherwise, go with a JSON lib.

我的2美分.

这篇关于从Java中的复杂JSON提取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆