3

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.

2
  • Wheres the code showing your current effort? Commented May 30, 2013 at 1:42
  • Saying you've looked around isn't enough. If you've spent hours and hours Googling, then what have you tried so far and what did you find that didn't work? Commented May 30, 2013 at 2:10

3 Answers 3

2

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 }
]
Sign up to request clarification or add additional context in comments.

Comments

0

You can pick a library (in this case, to decode JSON) from the Java section at the bottom of: http://json.org

1 Comment

I've tried multiple libraries at this point, all of which have given me a handful of problems. Currently I'm trying out gson. I'll edit the op to reflect that.
0

YOu can have a look to the library below: json-java

You can use the JSONObject to parse a String representing a JSON object and then extract the values you are interested in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.