Extract value using JSON Path from array
$.e.h.b[0]
looks like a JavaScript object (with fields including "ts" and "tz"), not a string. So it's reasonable for your JSON parser to treat it as a JSONObject rather than a string. Probably you need to drill down to $.e.h.b[0].ts
or whatever field you're interested in. Something like this:
long ts = JsonPath.read(pd, "$e.h.b[0].ts");
I'm assuming it's your second String ak =
line which causes the exception. I'm having trouble understanding your other question about the "ts" attributes. Maybe you could format the JSON string for easier reading?
user1677986
Updated on June 09, 2022Comments
-
user1677986 almost 2 years
I have Json like this below
{"pd":"{\"e\":{\"h\":{\"ak\":\"120\",\"at\":\"app\"},\"b\":[{\"ts\":1319549658547,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":8,\"ev\":\"sessionStart\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{\"day\":\"Tuesday\"}},{\"ts\":132,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":3,\"ev\":\"AutomaticFeedRefresh\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":131,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}}],\"tt\":{\"OSV\":\"7.10\"}}}","serverPayload":{"httpHeaders":{"x-bluecoat-via":["35D3468EFF4D5F18"],"content-type":["application\/x-www-form-urlencoded"]},"senderIp":["101.100.000.100"]}}
I just need the values of
ak
,b [ts,si and tt[day]]
andsenderIp
. Now I have 2 questions, how do I extract all 'ts' attributes in 'b' and 'senderIp'. I have used the below code for ak, ts and si. I am not sure how I get 'tt', also while I run this code I get an exception like belowString pd = JsonPath.read(jsonString, "$.pd"); String ak = JsonPath.read(pd, "$e.h.ak"); String ak = JsonPath.read(pd, "$e.h.b[0]"); // String b = JsonPath.read(pd,"$.e.b[0][0]"); // String b = JsonPath.read(pd,"$.e.b[0][5]"); System.out.println("value of ak: "+ak);
Exception in thread "main"
java.lang.ClassCastException: net.minidev.json.JSONObject cannot be cast to java.lang.String
. -
user1677986 over 11 yearsException in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
-
user1677986 over 11 years@ Hew Wolff TO make it simple, I want to extract values of ts,si, tt and 'senderIp' values
-
Hew Wolff over 11 years"ts" is an integer in the JSON, right? So you should try to get it as an integer type. If you were trying to get it as a string, that would explain the exception. I edited my answer to give a bit more detail on that.
-
user1677986 over 11 yearsthanks for your help, i ve got the answer... to get ts i should traverse to the object with array like b[0].ts and convert the value to string or use tostring() method
-
Hew Wolff over 11 yearsExcellent. If you care to accept my answer by clicking on the check mark, it will give me a warm feeling. :-)
-
emoleumassi over 8 yearsi have a similar issue, can you help me: stackoverflow.com/questions/35251927/…