I am trying to parse a json string to java but have got stuck. My code looks like the below.
public static void main(String args[]) throws FileNotFoundException, IOException, ParseException {
// parsing file "JSONExample.json"
Object obj = new JSONParser().parse(new FileReader("teams.json"));
// typecasting obj to JSONObject
JSONObject jo = (JSONObject) obj;
// getting address
Map teams = ((Map)jo.get("api"));
// iterating address Map
Iterator<Map.Entry> itr1 = teams.entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry pair = itr1.next();
System.out.println(pair.getKey() + " : " + pair.getValue());
}
And I get the following result.
teams : [{"arena":"Old Trafford","name":"Manchester United","team_id":33,"city":"Manchester"}]
results : 1
My teams.json
looks like following
{
"api": {
"results": 1,
"teams": [
{
"team_id": 33,
"name": "Manchester United",
"arena": "Old Trafford",
"city": "Manchester",
}
]
}
}
How do I continue if I just want the keys and values in the teams-array into an arraylist in java for example.