Android字符串到JSON数组错误

Android字符串到JSON数组错误,android,json,Android,Json,我正在解析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\

我正在解析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();
}
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;
试一试{
jsonarray=新jsonarray(str);
for(int i=0;i
我想将
JSONArray
转换为
String
,但JSONArray返回为空。 我在哪里?谢谢..

试试这个:

JSONArray arrayObj = null;
JSONParser jsonParser = new JSONParser();
object = jsonParser.parse(str);
arrayObj = (JSONArray) object;

使用JSONObject和JSONArray类来构造JSONArray,而不是将其作为字符串写入

参考试试这个

jsonarray = new JSONArray(str.replace("\\\"", "\"").replace("\n","").replaceAll("^\"|\"$", "").trim());

如果我理解正确,JSON字符串来自URL。@prudhvi您不知道如何在java字符串中转义引号吗?有任何异常吗?这个
JSONParser
类来自哪里?