0

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"
  }
}
2
  • you can use any JSON parser like Gson etc to do this Commented Aug 7, 2012 at 10:43
  • you can uses JSON API code.google.com/p/google-gson Commented Aug 7, 2012 at 10:44

3 Answers 3

2

Use json-path.

It's like xpath for JSON, and will allow you to write string queries on JSON objects. There are a lot of examples on the project site showing possible usages, but in your case it's probably just a simple dot notation.

An example for the provided JSON:

// First extract the CP value, as its JSON-string-inside-JSON:
String cp = JsonPath.read(yourJsonString, "$.CP");

// Treat the `cp` as another JSON-string, and extract the ak value:
String ak = JsonPath.read(cp, "$.e.h.ak");

// Do the rest yourself...
Sign up to request clarification or add additional context in comments.

Comments

2

You can use JsonPath to extract the value. I recommend JsonSurfer library.

<dependency>
    <groupId>com.github.jsurfer</groupId>
    <artifactId>jsurfer-simple</artifactId>
    <version>1.2.1</version>
</dependency>

The sample code solving your problem in two steps:

1) Extract plain string from "$.CP" node.

2) Parse the "CP" string and extract value for "ak", "dt" and "mi".

JsonSurfer jsonSurfer = JsonSurfer.simple();
String cp = jsonSurfer.collectOne(new StringReader(yourString), String.class, "$.CP");
Collection<Object> result = jsonSurfer.collectAll(new StringReader(cp), "$..ak", "$..dt", "$..mi");

Comments

1

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);

This prints out the value for "ak".

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

My 2 cents.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.