我在URL中解析json字符串。
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();
}
我希望将JSONArray
转换为String
,但JSONArray会返回空白。
我错在哪里?谢谢..
答案 0 :(得分:0)
使用JSONObject和JSONArray类构造JSONArray,而不是将其编写为String。
答案 1 :(得分:0)
试试这个
jsonarray = new JSONArray(str.replace("\\\"", "\"").replace("\n","").replaceAll("^\"|\"$", "").trim());
答案 2 :(得分:-1)
试试这个:
JSONArray arrayObj = null;
JSONParser jsonParser = new JSONParser();
object = jsonParser.parse(str);
arrayObj = (JSONArray) object;