2016-11-17 216 views
1

我正在解析包含首要排名表的鏈接中的一些數據。我喜歡用它在列表視圖中顯示。但是我得到JSONException和JSON.typeMismatch錯誤。org.json.JSONException:在org.json.JSON.typeMismatch處的值

下面是我的代碼:

public class ParseJSON { 
    public static String[] position1; 
    public static String[] team; 
    public static String[] points; 
    public static final String JSON_ARRAY = "data"; 
    public static final String CHILD_ARRAY = "standings"; 
    public static final String CHILDS_CHILD_ARRAY = "overall"; 
    public static final String KEY_POSITION = "position"; 
    public static final String KEY_TEAM = "team"; 
    public static final String KEY_POINTS = "points"; 
    private JSONArray users = null, user2=null; 
    private String json; 
    public ParseJSON(String json){ 
     this.json = json; 
    } 
    protected void parseJSON(){ 
     JSONObject jsonObject=null, jsonObject1=null; 
     try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONArray(JSON_ARRAY); 
      try { 
       jsonObject1=new JSONObject(json); 
       user2=jsonObject1.getJSONArray(CHILD_ARRAY); 
       position1 = new String[user2.length()]; 
       team = new String[user2.length()]; 
       for(int i=0;i<user2.length();i++){ 
        JSONObject jo = user2.getJSONObject(i); 
        position1[i] = jo.getString(KEY_POSITION); 
        team[i] = jo.getString(KEY_TEAM); 
        /*points[i] = jo.getString(KEY_POINTS);*/ 
       } 
      }catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      points = new String[users.length()]; 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }}} 

我收到錯誤消息

> W/System.err: org.json.JSONException: Value {"standings":[{"identifier":"5hichhljwlipyqvghhr0banvd2gehtcf","position":1,"team_identifier":"akppwaoizlxbsa66oupfkawevutbnjxp","team":"Liverpool","overall":{"wins":8,"draws":2,"losts":1,"points":26,"scores":30,"..."","matches_p 
W/System.err:  at org.json.JSON.typeMismatch(JSON.java:100) 
W/System.err:  at org.json.JSONObject.getJSONArray(JSONObject.java:588) 
W/System.err:  at d33pzz.example.com.dataparsingexample.ParseJSON.parseJSON(ParseJSON.java:37) 
W/System.err:  at d33pzz.example.com.dataparsingexample.MainActivity.showJSON(MainActivity.java:61) 
W/System.err:  at d33pzz.example.com.dataparsingexample.MainActivity.access$000(MainActivity.java:17) 
W/System.err:  at d33pzz.example.com.dataparsingexample.MainActivity$1.onResponse(MainActivity.java:44) 
W/System.err:  at d33pzz.example.com.dataparsingexample.MainActivity$1.onResponse(MainActivity.java:41) 
W/System.err:  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 
W/System.err:  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 
W/System.err:  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
W/System.err:  at android.os.Handler.handleCallback(Handler.java:746) 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
W/System.err:  at android.os.Looper.loop(Looper.java:148) 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5443) 
W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

任何人都可以請指導我哪裏做錯了嗎?這JSON格式如何解析和顯示:

{ 
"data":{ 
    "standings":[ 
     { 
      "identifier":"5hichhljwlipyqvghhr0banvd2gehtcf", 
      "position":1, 
      "team_identifier":"akppwaoizlxbsa66oupfkawevutbnjxp", 
      "team":"Liverpool", 
      "overall":{ 
       "wins":8, 
       "draws":2, 
       "losts":1, 
       "points":26, 
       "scores":30, 
       "conceded":14, 
       "last_5":"", 
       "matches_played":11, 
       "goal_difference":16 
      }, 
      "home":{ 
       "wins":4, 
       "draws":1, 
       "losts":0, 
       "scores":17, 
       "conceded":4, 
       "points":13, 
       "last_5":"", 
       "matches_played":5, 
       "goal_difference":13 
      }, 
      "away":{ 
       "wins":4, 
       "draws":1, 
       "losts":1, 
       "scores":13, 
       "conceded":10, 
       "points":13, 
       "last_5":"", 
       "matches_played":6, 
       "goal_difference":3 
      }, 
      "penalization_points":0 
     }, 
{ 

    "identifier":"psnb5ewiti9ejktz9w7v0lhgq06eorih", 
    "position":2, 
    "team_identifier":"blfamr89lxeyywtsraiqzq5p5zuz57i6", 
    "team":"Chelsea", 
    "overall":{ 
     "wins":8, 
     "draws":1, 
     "losts":2, 
     "points":25, 
     "scores":26, 
     "conceded":9, 
     "last_5":"", 
     "matches_played":11, 
     "goal_difference":17 
    }, 
    "home":{ 
     "wins":5, 
     "draws":0, 
     "losts":1, 
     "scores":18, 
     "conceded":3, 
     "points":15, 
     "last_5":"", 
     "matches_played":6, 
     "goal_difference":15 
    }, 
    "away":{ 
     "wins":3, 
     "draws":1, 
     "losts":1, 
     "scores":8, 
     "conceded":6, 
     "points":10, 
     "last_5":"", 
     "matches_played":5, 
     "goal_difference":2 
    }, 
    "penalization_points":0 
} 
    ], 
    "statusCode":"200", 
    "errorCode":"0", 
    "statusReason":"OK" 
}} 
+0

「數據」 ID的JSONObject不JSONArray。 –

回答

0

我找到了解決方案。

JSON解析器類:

public class ParseJSON { 

     public static String[] position1; 
     public static String[] team; 
     public static String[] points; 

     public static final String JSON_ARRAY = "data"; 

     public static final String CHILD_ARRAY = "standings"; 

     public static final String KEY_ID = "position"; 
     public static final String KEY_NAME = "team"; 

     private JSONObject users = null; 
     private JSONArray user2=null; 
     private JSONObject user3=null; 

     private String json; 

     public ParseJSON(String json){ 
      this.json = json; 
     } 
     protected void parseJSON() { 
      JSONObject jsonObject = null; 
      try { 
       jsonObject = new JSONObject(json); 
       users = jsonObject.getJSONObject(JSON_ARRAY); 
       try { 
        user2=users.getJSONArray(CHILD_ARRAY); 
        position1 = new String[user2.length()]; 
        team = new String[user2.length()]; 
        points=new String[user2.length()]; 
        for (int i = 0; i < user2.length(); i++) { 

         JSONObject jo = user2.getJSONObject(i); 
         try { 
          user3=jo.getJSONObject("overall"); 
          points[i] = user3.getString("points"); 
          System.out.println("Message me: "+points[i]); 
         }catch (Exception e) 
         { 
          e.printStackTrace(); 
         } 
         position1[i] = jo.getString(KEY_ID); 
         team[i] = jo.getString(KEY_NAME); 
         System.out.println("Message me: "+position1[i]); 
         System.out.println("Message me: "+team[i]); 


        } 
       }catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
0

您正在JSONObject到JSONArray這是錯誤的。

users = jsonObject.getJSONArray(JSON_ARRAY); 

變化從JSONArray到JSONObject的這樣的用戶數據的類型和更新上面一行:

users = jsonObject.getJSONObject(JSON_ARRAY); 
0

jsonObject = new JSONObject(json);你有一個JSON對象data,而不是一個JSON數組。因此,嘗試了這一點

jsonObject = new JSONObject(json); 
JSONObject standings = jsonObject.getJSONObject("data"); 
//Continue with the code 
0

你總是會得到錯誤,因爲 「數據」 的JSONObject不JSONArray

用戶= jsonObject.getJSONArray(JSON_ARRAY);

您的JSON,其中數據的JSONObject

{ 
"data":{ 
    "standings":[ 
     { 
      "identifier":"5hichhljwlipyqvghhr0banvd2gehtcf", 
      "position":1, 
      "team_identifier":"akppwaoizlxbsa66oupfkawevutbnjxp", 
      "tea 
0

我修改了代碼。請遵照它。

public class ParseJSON { 
    public static String[] position1; 
    public static String[] team; 
    public static String[] points; 
    public static final String JSON_OBJECT_DATA = "data"; 
    public static final String CHILD_ARRAY = "standings"; 
    public static final String CHILDS_CHILD_ARRAY = "overall"; 
    public static final String KEY_ID = "position"; 
    public static final String KEY_NAME = "team"; 
    public static final String KEY_EMAIL = "points"; 
    private JSONObject users = null; 
    private JSONArray user2=null; 
    private String json; 
    public ParseJSON(String json){ 
     this.json = json; 
    } 
    protected void parseJSON(){ 
     JSONObject jsonObject=null, jsonObject1=null; 
     try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONObject(JSON_OBJECT_DATA); 
      try { 

       user2=users.getJSONArray(CHILD_ARRAY); 
       position1 = new String[user2.length()]; 
       team = new String[user2.length()]; 
       for(int i=0;i<user2.length();i++){ 
        JSONObject jo = user2.getJSONObject(i); 
        position1[i] = jo.getString(KEY_ID); 
        team[i] = jo.getString(KEY_NAME); 
       /*points[i] = jo.getString(KEY_EMAIL);*/ 
       } 
      }catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      points = new String[users.length()]; 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }}} 
相關問題