【问题标题】:JSON type mismatch or org.json.jsonecxeptionJSON 类型不匹配或 org.json.jsonecxeption
【发布时间】:2013-12-18 08:32:53
【问题描述】:

链接是 http://iipacademy.in/askpoll/ten_feed.php

onPostExecute() 方法中有异常(第 4 行):

Log.i("result", result);
try {
    if (result != null) {
        JSONArray jsonArray = new JSONArray(result); // erreor
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject objJson = jsonArray.getJSONObject(i);

            TopTenGetterSetter obj = new TopTenGetterSetter();  


            obj.setQ(objJson.getString("question"));
            obj.setA(objJson.getString("option1"));
            obj.setB(objJson.getString("option2"));
            obj.setC(objJson.getString("option3"));
            obj.setD(objJson.getString("option4"));

            polls.add(obj);
                        }

    }
} catch (JSONException e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), "error",
            Toast.LENGTH_SHORT).show();
}

LOGCAT:

12-18 03:20:45.447: W/System.err(2790): org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray
12-18 03:20:45.447: W/System.err(2790):     at org.json.JSON.typeMismatch(JSON.java:111)
12-18 03:20:45.447: W/System.err(2790):     at org.json.JSONArray.<init>(JSONArray.java:91)
12-18 03:20:45.447: W/System.err(2790):     at org.json.JSONArray.<init>(JSONArray.java:103)
12-18 03:20:45.447: W/System.err(2790):     at com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:188)
12-18 03:20:45.447: W/System.err(2790):     at com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:1)
12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask.finish(AsyncTask.java:631)
12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-18 03:20:45.447: W/System.err(2790):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 03:20:45.447: W/System.err(2790):     at android.os.Looper.loop(Looper.java:137)
12-18 03:20:45.447: W/System.err(2790):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-18 03:20:45.447: W/System.err(2790):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 03:20:45.447: W/System.err(2790):     at java.lang.reflect.Method.invoke(Method.java:525)
12-18 03:20:45.447: W/System.err(2790):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-18 03:20:45.447: W/System.err(2790):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 03:20:45.447: W/System.err(2790):     at dalvik.system.NativeStart.main(Native Method)
12-18 03:20:45.447: D/dalvikvm(2790): GC_FOR_ALLOC freed 5131K, 55% free 4437K/9672K, paused 2ms, total 2ms

消息是一个数组,那么它的代码应该是什么或如何解决?

谢谢 在 进步 。 . .

【问题讨论】:

  • 给定的 web 服务返回 JSONObject 作为根元素而不是 JSONArray
  • 你可以使用类似new org.json.simple.parser.JSONParser().parse("jsonString");的东西来获取JsonArray。

标签: java android json android-asynctask


【解决方案1】:
org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray

看起来响应是一个字符串而不是一个 json 数组

{  // json object node 
    "response": { // json object response
        "result": 1,
        "Message": [ // json array Message
            {        // json object node 
                "pollid": "98",
                 "category": "Entertainment",
                 "question": "what",  //  string
                 "option1": "981.mov",

结果是一个json对象而不是json数组

JSONArray jsonArray = new JSONArray(result);

应该是

JSONObject jObj = new JSONObject(result);
JSONObject response = jObj.getJSONObject("response");
//JSONObject jb = new JSONObject(response);
JSONArray jr = response.getJSONArray("Message");
for(int i=0;i<jr.length();i++)
{
JSONObject jb1 = jr.getJSONObject(i);
String question = jb1.getString("question");
Log.i(".......",question);
}

【讨论】:

    【解决方案2】:

    Result 不是数组,Message 是。

    【讨论】:

      【解决方案3】:

      试试这个:

      Log.i("result", result);
      try {
          if (result != null) {
              JSONObject jObject = new JSONObject(result);
              JSONArray jsonArray = jObject.getJSONObject("response").getJSONArray("Message");
              for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject objJson = jsonArray.getJSONObject(i);
      
                  TopTenGetterSetter obj = new TopTenGetterSetter();  
                  /*
                   * [{"job_id":"1","job_staff_id":"","job_name":"Account",
                   * "job_detail":"test\r\ntesds","job_start_date":
                   * "2013-11-08"
                   * ,"job_end_date":"2013-11-10","job_amount":"500"
                   * ,"job_progress":"","job_complete_status":"0"}]
                   */
      
                  obj.setQ(objJson.getString("question"));
                  obj.setA(objJson.getString("option1"));
                  obj.setB(objJson.getString("option2"));
                  obj.setC(objJson.getString("option3"));
                  obj.setD(objJson.getString("option4"));
      
                  polls.add(obj);
                  // am = Customer.info.get(pos).getJamount();
      
                  // Toast.makeText(getApplicationContext(), am + result,
                  // Toast.LENGTH_LONG).show();
              }
      
          }
      } catch (JSONException e) {
          e.printStackTrace();
          Toast.makeText(getApplicationContext(), "error",
                  Toast.LENGTH_SHORT).show();
      }
      

      正如 mario 和 nfear 所说,您正在尝试将 JSONObject 转换为 JSONArray。

      【讨论】:

        【解决方案4】:

        字符串内容 JSONObject 作为根元素而不是 JSONArray。要从 String 中获取 Message JSONArray,您应该首先获取 response JSONObject,然后将 Message JSONArray 获取为:

         JSONObject jsonobj = new JSONObject(result);
         // get response JSONObject
        
         JSONObject jsonobj_response = jsonobj.getJSONObject("response");
        
        // get Message JSONArray from jsonobj_response
        
         JSONArray jsonArray = jsonobj_response.getJSONArray("Message");
          // your code here.....
        

        【讨论】:

          【解决方案5】:

          试试这个,

          try {
              if (result != null) {
                  JSONArray jsonArray = new JSONObject(result).getJSONObject("response").getJSONArray("Message");
                  for (int i = 0; i < jsonArray.length(); i++) {
                      JSONObject objJson = jsonArray.getJSONObject(i);
          
                      TopTenGetterSetter obj = new TopTenGetterSetter();  
                      /*
                       * [{"job_id":"1","job_staff_id":"","job_name":"Account",
                       * "job_detail":"test\r\ntesds","job_start_date":
                       * "2013-11-08"
                       * ,"job_end_date":"2013-11-10","job_amount":"500"
                       * ,"job_progress":"","job_complete_status":"0"}]
                       */
          
                      obj.setQ(objJson.getString("question"));
                      obj.setA(objJson.getString("option1"));
                      obj.setB(objJson.getString("option2"));
                      obj.setC(objJson.getString("option3"));
                      obj.setD(objJson.getString("option4"));
          
                      polls.add(obj);
                      // am = Customer.info.get(pos).getJamount();
          
                      // Toast.makeText(getApplicationContext(), am + result,
                      // Toast.LENGTH_LONG).show();
                  }
          
              }
          } 
          

          【讨论】:

            【解决方案6】:

            您发布的 JSON 有

            // Key "response" , Value type JsonObject
            
             JSONObject jsonObject=jsonObjectOfRsponse.getJsonObject(key);
            
            //Key "Message" , Value type Json Array
            
             JSONArray jsonArray=jsonObject.getJsonArray(key);
            

            得到jsonArray后,循环使用this,根据里面的value类型解析Json。

            所以检查你是否根据它所持有的值的类型来解析 json。

            【讨论】:

              【解决方案7】:
              Value response of type java.lang.String cannot be converted to JSONArray
              

              您正在尝试将单个对象转换为数组。

              【讨论】:

                【解决方案8】:

                最好使用 GSON 库而不是手动解析它。 GSON 你可以在 goggling 上找到它。并给出了示例。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-01-22
                  • 2020-12-12
                  • 2014-10-31
                  • 1970-01-01
                  • 2012-08-10
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多