-2

I am trying to parse a JSON Array But I Dont know how to parse such Array. Please explain the concept when you write the code

{
  "snappedPoints": [
    {
      "location": {
        "latitude": -35.2784167,
        "longitude": 149.1294692
      },
      "originalIndex": 0,
      "placeId": "ChIJoR7CemhNFmsRQB9QbW7qABM"
    },
    {
      "location": {
        "latitude": -35.280321693840129,
        "longitude": 149.12908274880189
      },
      "originalIndex": 1,
      "placeId": "ChIJiy6YT2hNFmsRkHZAbW7qABM"
    },
    {
      "location": {
        "latitude": -35.280960897210818,
        "longitude": 149.1293250692261
      },
      "originalIndex": 2,
      "placeId": "ChIJW9R7smlNFmsRMH1AbW7qABM"
    },
    {
      "location": {
        "latitude": -35.28142839817933,
        "longitude": 149.1298619971291
      },
      "originalIndex": 3,
      "placeId": "ChIJy8c0r2lNFmsRQEZUbW7qABM"
    },
    {
      "location": {
        "latitude": -35.28193988170618,
        "longitude": 149.13001013387623
      },
      "originalIndex": 4,
      "placeId": "ChIJ58xCoGlNFmsRUEZUbW7qABM"
    },
    {
      "location": {
        "latitude": -35.282819705480151,
        "longitude": 149.1295597114644
      },
      "originalIndex": 5,
      "placeId": "ChIJabjuhGlNFmsREIxAbW7qABM"
    },
    {
      "location": {
        "latitude": -35.283139388422363,
        "longitude": 149.12895618087012
      },
      "originalIndex": 6,
      "placeId": "ChIJ1Wi6I2pNFmsRQL9GbW7qABM"
    },
    {
      "location": {
        "latitude": -35.284728724835304,
        "longitude": 149.12835061713685
      },
      "originalIndex": 7,
      "placeId": "ChIJW5JAZmpNFmsRegG0-Jc80sM"
    }
  ]
}

can someone please write the code for this one and explain it?

Thanks in advance

I Tried this one for testing whether it fetch records or not?

 JSONArray array = null;
        try {
           array = new JSONArray("snappedPoints");
            for(int i =0; i<= array.length();i++) {
                JSONObject object = array.getJSONObject(i);
                String Location = (String) object.get("latitude");
                Toast.makeText(MainActivity.this, Location, Toast.LENGTH_SHORT).show();
                Log.e("============" ,Location );
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
Akshay Sood
  • 152
  • 1
  • 3
  • 15

1 Answers1

1

Ok first i gonna writte the code in sample JSON library from Java.

String jsonString = "here your json";

JSONObject json = new JSONObject(jsonString);
JSONArray snappedPoints = json.getJSONArray("snappedPoints");

for (int i = 0; i < snappedPoints.lenght(); i++){
    JSONObject location = snappedPoints.getJSONObject(i).getJSONObject("location");

    double latitude = location.getDouble("latitude");
    double longitude = location.getDouble("longitude");
}

Second i recomend you to use some library like, Jackson, Gson, etc.., to a easies way to convert json into POJOs.

Edit:

Using RetroFit and Gson:

Frist we declare some simple pojos:

    public class SnappedPoint {
        private Location location;
        private int originalIndex;
        private String placeId;

        public Location getLocation() {
            return location;
        }
        public void setLocation(Location location) {
            this.location = location;
        }
        public int getOriginalIndex() {
            return originalIndex;
        }
        public void setOriginalIndex(int originalIndex) {
            this.originalIndex = originalIndex;
        }
        public String getPlaceId() {
            return placeId;
        }
        public void setPlaceId(String placeId) {
            this.placeId = placeId;
        }

    }


    public class Location {
        private double latitude;
        private double longitude;

        public double getLatitude() {
            return latitude;
        }

        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }
    }

After that into your RetroFit Interface declaration you put the next callback:

CallBack< HashMap<String, ArrayList<SnappedPoint>> >

You should get aHashMap<String, ArrayList<SnappedPoint>> response to get your location info you only have to:

ArrayList<SnappedPoint> snappedPoints = response.get("snappedPoints");
for (SnappedPoint snappedPoint : snappedPoints){
    double latitude = snappedPoint.getLocation().getLatitude();
    double longitude = snappedPoint.getLocation().getLongitude();
}