Answer the question
In order to leave comments, you need to log in
How to get element from json array in java associatively (vk longpool)?
When I contact the VKontakte server to get data for a long pool request, I get the following response:
Array
(
[response] => Array
(
[key] => 3frf4b6e17478cеf5c463c4f14a5afcd93208412a
[server] => https://lp.vk.com/wh123264621
[ts] => 15
)
)
import org.json.*;
public class test123 {
public static void main(String args[]){
String json = "{"response":{"key":"12312","server":"12312312","ts":"231"}}"; // (with `\` before ` " ` )
JSONObject o = new JSONObject(json);
JSONArray a = o.getJSONArray("response");
String out = (String) a.get("key");
System.out.println(out);
}
}
Answer the question
In order to leave comments, you need to log in
If you need to get not the whole object, but some nodes:
String json = "{"response":{"key":"12312","server":"12312312","ts":"231"}}";
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
String key = jsonNode.get("key").asText();
ObjectMapper mapper = new ObjectMapper()
YourObject obj = mapper.readValue(jsonString, YourObject.class);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question