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

3
当我运行这个程序时,出现了这个错误。我不知道该如何解决它。请帮我找到解决方法。
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 class Http
{

public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;
private static HttpClient getHttpClient() {

      if (mHttpClient == null) {
       mHttpClient = new DefaultHttpClient();

       final HttpParams params = mHttpClient.getParams();
       HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
       HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
       ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
      }

      return mHttpClient;
     }


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());



            // Parse the JSON String into a JSONArray object.
            return JSONArray(jsonString);

        } 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;

    }



public static JSONArray retrieveJSON(){
    {
          StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork()                  .penaltyLog().build());

        String getAllFreebiesURL="http://10.0.2.2/football365/cityList.php";
        JSONArray json = null;
        try {
            json = getJSONArrayFromUrl(getAllFreebiesURL);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("JSON",json+"A");
        //JSONArray json1 = new JSONArray(json);
        //json1.put(json);
        /*try {
            System.out.println(json1.get(2));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        return json;
    }
 }
 }

你在 log 中获取到了 json 吗? - Hariharan
看起来是一个字符编码问题。 - Scary Wombat
@user2310289,请展示一下你所获得的JSON响应类型。 - Aamirkhan
@ Tamilan。是的,当我在日志中输出JSON时得到了这个错误。 - user3032822
@user3032822,请发布您的Logcat错误日志。 - Hariharan
3个回答

0

将您的方法更改为:

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;

}

0

你不能将字符串转换为 JsonArray,首先需要将其转换为 JsonObject

try {
       JSONObject jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

然后从中获取JsonArray

jObj.getJSONArray(NAME);


当我这样做时,Java.lang.String 无法转换为 JsonObject。那是我得到的错误。 - user3032822

-1

以下是尝试的方法: 您可以按照以下方式解析响应:

    // 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.
               }

请将您的getJSONArrayFromurl()更新如下:

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;

    }

我不能使用循环。这是我的领导说的。我只得到了CityID。你能帮我吗? - user3032822
不使用循环,你如何获取所有的城市ID? - GrIsHu
那就是我告诉她的。但是她命令我使用类似字典对象或其他东西 :( - user3032822
只有通过循环才能获取响应中的所有CityID,这是唯一可能的。这是常识啊。 - GrIsHu
我真的很抱歉。谢谢您的耐心。 - user3032822
1
不用道歉,随时乐意帮忙 :) - GrIsHu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接