1

I am parse json string in URL.

TextView tv3 = (TextView) findViewById(R.id.tv3);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TextView tv1 = (TextView) findViewById(R.id.tv1);

String str = "[{\"ID\":\"1\",\"lat\":\"40.7646815\",\"lon\":\"29.030855\"},{\"ID\":\"2\",\"lat\":\"41.10812\",\"lon\":\"29.030855\"}]";


JSONArray jsonarray = null;
try {

    jsonarray = new JSONArray(str);
    for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject obj = jsonarray.getJSONObject(i);

        String lat = obj.getString("lat");
        String lon = obj.getString("lon");
        String id = obj.getString("ID");

        tv3.setText(lat);
        tv2.setText(lon);
        tv1.setText(ID);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

I want converting JSONArray to String but JSONArray returning empty. Where I'm Error ? Thank You..

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sunay yildiz
  • 79
  • 2
  • 2
  • 13

3 Answers3

0

Use JSONObject and JSONArray classes to construct JSONArray, instead of writing it as a String.

Refer https://stackoverflow.com/a/17810270/4586742

Community
  • 1
  • 1
Bob
  • 13,447
  • 7
  • 35
  • 45
0

try this

jsonarray = new JSONArray(str.replace("\\\"", "\"").replace("\n","").replaceAll("^\"|\"$", "").trim());
Rafael Mota
  • 127
  • 4
-1

Try this :

JSONArray arrayObj = null;
JSONParser jsonParser = new JSONParser();
object = jsonParser.parse(str);
arrayObj = (JSONArray) object;
Ben-J
  • 1,084
  • 8
  • 24
yozzy
  • 344
  • 2
  • 15