无法将java.lang.String转换为JSONArray

无法将java.lang.String转换为JSONArray,java,android,json,Java,Android,Json,当我运行这个程序时,我得到了这个错误。我不知道怎么解决。请帮我找到它 12-02 23:04:34.427: E/JSON Parser(1629): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray 代码: 将您的方法更改为: public static JSONArray getJSONArrayFromUrl(Stri

当我运行这个程序时,我得到了这个错误。我不知道怎么解决。请帮我找到它

12-02 23:04:34.427: E/JSON Parser(1629): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray
代码:


将您的方法更改为:

public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
try {


       HttpClient client = getHttpClient();

       HttpGet request = new HttpGet();

       request.setURI(new URI(url));

       HttpResponse response = client.execute(request);

    try {
        // Get our response as a String.
        String jsonString = EntityUtils.toString(response.getEntity());
        JSONObject jsonObject = new JSONObject(jsonString);
        String[] names = JSONObject.getNames(jsonObject);
        JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));


        // Parse the JSON String into a JSONArray object.
        return jsonArray;

    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return null;

}

您不能将字符串强制转换为
JsonArray
,首先将其强制转换为
JsonObject

try {
       JSONObject jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
然后从中获取
JsonArray

jObj.getJSONArray(名称)

按如下方式进行尝试: 您可以按如下方式解析您的响应:

    // Get our response as a String.
    String jsonString = EntityUtils.toString(response.getEntity());
   JSONObject m_jobj;
try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    for (int i = 0; i < m_ja.length(); i++) 
     {
                 JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }
public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
    try {


           HttpClient client = getHttpClient();

           HttpGet request = new HttpGet();

           request.setURI(new URI(url));

           HttpResponse response = client.execute(request);

        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(response.getEntity());
       JSONObject m_jobj;
    try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    /*for (int i = 0; i < m_ja.length(); i++) 
             {
             JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }*/


            // Parse the JSON String into a JSONArray object.
            return m_ja;

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

    }

您是否在日志中获取了
json
。看起来像字符编码problem@user2310289显示您在@Tamilan获得的json响应类型。是的,这就是我在日志中输出json时遇到的错误。@user3032822发布您的logcat错误。当我这样做时,JAva.lang.String无法转换为JSonObject。这就是我的错误,我不能用loopin。这是我的领导说的。我得到的只是城市ID。你能帮帮我吗?不重复你怎么能弄到所有的城市?这就是我对她说的。但她命令我使用字典对象之类的东西:(只有当你想从你的回答中获得所有的城市ID时,才可能使用循环。这是一个常识性的人。不要抱歉。总是乐于帮助。)
public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
    try {


           HttpClient client = getHttpClient();

           HttpGet request = new HttpGet();

           request.setURI(new URI(url));

           HttpResponse response = client.execute(request);

        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(response.getEntity());
       JSONObject m_jobj;
    try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    /*for (int i = 0; i < m_ja.length(); i++) 
             {
             JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }*/


            // Parse the JSON String into a JSONArray object.
            return m_ja;

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

    }