Reputation: 11
On the following json, i'd like to get the elements and put every row in a different string, so i made a List. My problem is that with my snipper i can't get the, for example, Name="name1" and Weight="2" on the same row.
string = '{"Name":"name1", "Weight":"3"},{"Name":"name2","Weight":"2"}'
JSONArray jsonarray = (JSONArray) parser.parse(string);
List list = new ArrayList();
Iterator<JSONObject> iterator = jsonarray.iterator();
while (iterator.hasNext()) {
list.add(iterator.next().get("Name")+"/"+iterator.next().get("Weight"));
}
I need something like ["name1/3","name2/2"]. I know that when i type iterator.next() twice, like i did, i will go on the next element too soon, but i don't know how to do exactly.
Thank you in advance!
Upvotes: 0
Views: 137
Reputation: 97152
Assign the result of the first call to next()
to an object:
while (iterator.hasNext()) {
JSONObject object = iterator.next();
list.add(object.get("Name") + "/" + object.get("Weight"));
}
Upvotes: 1