0
{
    "result": [
        {
            "id": "a258377906705d889422fd0b41c324b8",
            "coordinate": {
                "London": {
                    "x": 65.565709,
                    "y": 98.931235
                },
                "New_York": {
                    "x": 37.59751,
                    "y": 47.448718
                }
            }
        }
    ]
}

If I have a json like the above one, how can I loop to get the x,y coordinate and if i get more data to get, how can I get it?

If I want to also get the London and New York to add to an array list, how can I do it (cannot directly add the name because there are more than two data)?

Draken
  • 3,134
  • 13
  • 34
  • 54
Ng Clement
  • 45
  • 8

2 Answers2

0

You can parse it and then use it using JSON.parse( insert json here ).

Basically what it does is that it takes your JSON string and converts it into a usable JSON.

For adding new objects into another object, I recommend using Object.assign(). You can refer the details here: Link

Here is an example given from the site:

var o1 = { a: 1 };
var o2 = { b: 2 };
var o3 = { c: 3 };

var obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1);  // { a: 1, b: 2, c: 3 }, target object itself is changed.

Object.assign() takes a target object as the first parameter and modifies it with the additional parameters that we're provided.

In your case, you can update your object like this:

var countryCoord = JSON.parse(yourjsondata); // parse the json that you included
 Object.assign(countryCoord, newCoord, newCoord2,...etc); //this would update countryCoord alone

Online parser: Link

Joshua Rajandiran
  • 2,788
  • 7
  • 26
  • 53
0

You have to handle the json and understand where you are getting the JSONObject and where you are getting the JSONArray, On basis of that you can parse the JSON accordingly.

Please refer the link for more information on parsing json object with nested items in Java:

Retrieving nested arrays values with java

Community
  • 1
  • 1
Rishal
  • 1,480
  • 1
  • 11
  • 19