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"
}
}
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:
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.