Need help with formatted json string. (Java)

I have the contents of this page stored in a string variable. I spent several hours on various methods of working with json in java and learned a lot about the build path, dependencies, ssl certificates, etc. I say this to emphasize the fact that I was looking for the answer to this question myself. I finally collapsed and I need help.

I am looking for a way to turn this string into some kind of json array so that I can access individual elements more easily. For example, something like:

int map = jsonArray[index].getInt("map_Id"); 

I hope that what I'm trying to do is clear enough. Any help would be appreciated.

edit: I am currently trying to do this using gson, but I am open to suggestions.

+4
source share
3 answers

You can use the Jackson libraries used to parse JSON as follows:

 public static int getIntFromJson(String jsonString) { ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(jsonString); int id = node.get(0).get("age").asInt(); return id; } 

Assuming your jsonString will be something similar, the above code will return 39 .

 [ { "name": "Dagny Taggart", "age": 39 }, { "name": "Francisco D'Anconia", "age": 40 }, { "name": "Hank Rearden", "age": 46 } ] 
+2
source

You can select the library (in this case, for JSON decoding) from the Java section below: http://json.org

+1
source

You can look into the library below: json-java

You can use JSONObject to parse the string representing the JSON object, and then extract the values โ€‹โ€‹of interest to you.

0
source

Source: https://habr.com/ru/post/1483491/


All Articles