I have the contents of this page stored in a string variable. I've spent hours looking at different ways of working with json in java, and have learned a lot about the build path, dependencies, ssl certificates, and so on. I say that to emphasize the fact that I have been looking for an answer to this question independently. I've finally caved and I need help.
I'm looking for a way to turn this string into some kind of json array so that I could access the individual elements more easily. For example, something along the lines of:
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'm currently attempting this with gson but am open to suggestions.
You can use the Jackson libraries used for JSON parsing 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 that your jsonString will be something like this, the above code will return39.
[
{ "name": "Dagny Taggart", "age": 39 },
{ "name": "Francisco D'Anconia", "age": 40 },
{ "name": "Hank Rearden", "age": 46 }
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With