-3

How to fetch latitude and longitude from this json data in java?

{
"cloc": {
    "cid": 1001,
    "latlong": [
        {
            "lat": 23.2331,
            "lon": 43.23
        },
        {
            "lat": 44.322,
            "lon": 93.423
        },
        {
            "lat": 44.322,
            "lon": 93.423
        }
    ]
}
}
Maveňツ
  • 1
  • 12
  • 50
  • 89
Rahul
  • 1,380
  • 1
  • 10
  • 24

2 Answers2

1

Try with below code:

JSONObject object = new JSONObject(YOUR RESPONSE STRING);

JSONArray jArray = object.getJSONArray("latlong");

for (int i = 0; i < jArray.length(); i++)
{
    String lat = jArray.getJSONObject(i).getString("lat");
    String lon = jArray.getJSONObject(i).getString("lon");

}
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • Exception in thread "main" org.json.JSONException: JSONObject["latlong"] not found. at org.json.JSONObject.get(JSONObject.java:454) at org.json.JSONObject.getJSONArray(JSONObject.java:535) at com.tatya.JsonReader.main(JsonReader.java:45) – Rahul Nov 19 '14 at 05:52
  • @Rah show me how you have implemented? – Pratik Dasa Nov 19 '14 at 05:57
  • How you have wrote this line in your code : JSONObject object = new JSONObject(YOUR RESPONSE STRING); – Pratik Dasa Nov 19 '14 at 05:58
  • JSONObject object = readJsonFromUrl("http://192.168.0.10:8080/project/Child?id1=1001");public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); //JSONArray json = new JSONArray(jsonText); return json; } – Rahul Nov 19 '14 at 06:27
  • @Rah its not the issue in my code, you are doing something wrong, please check it out. – Pratik Dasa Nov 19 '14 at 06:30
  • I tried this one object.getJSONObject("cloc").getJSONArray("latlong").getJSONObject(1) but it gives me only {"lon":93.423,"lat":44.322} and i need lat and long – Rahul Nov 19 '14 at 06:32
  • @Rah yah then you are going on right way. JSONArray json = object.getJSONObject("cloc").getJSONArray("latlong"); Then, go with for loop as I have posted in my answer. – Pratik Dasa Nov 19 '14 at 06:34
  • Why this exception is happening org.json.JSONException: JSONObject["lat"] not a string. do you have some clue. please – Rahul Nov 19 '14 at 06:41
  • Try with this way : object.getJSONObject("cloc").getJSONArray("latlong").getJSONObject(1).getString("lat"); and tell me is it working or not? – Pratik Dasa Nov 19 '14 at 06:43
  • String lat = json.getJSONObject(i).get("lat").toString(); Thanks a lot, Finalyy its worked for me – Rahul Nov 19 '14 at 06:45
  • @Rah I am asking that is it working or not? " not its working." means? – Pratik Dasa Nov 19 '14 at 07:01
0

Assuming that your data is already a parsed JSONObject, you can use:

JSONData.getJSONObject("cloc").getJSONArray("latlong").getJSONObject(i).getString("lat")

and:

JSONData.getJSONObject("cloc").getJSONArray("latlong").getJSONObject(i).getString("lon")

iterating over all the values of i in the range from 0 till the length of the latlong array.

Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53
  • I tried this one object.getJSONObject("cloc").getJSONArray("latlong").getJSONObject(1) but it gives me only {"lon":93.423,"lat":44.322} and i need lat and long – Rahul Nov 19 '14 at 06:32