android:jsonArray使用Array.sort进行解析

时间:2013-12-05 13:40:15

标签: android arrays json parsing

我的代码工作正常。但它很复杂......

我通过json

获取数据
try{
            JSONArray matchdata = null;
            matchdata = json.getJSONArray("matchdata");

我想只过滤完成的匹配

for(int i=0;i<matchdata.length();i++){
                    JSONObject e = matchdata.getJSONObject(i);
                    //only read finished Matchs  
                    if (e.getBoolean("match_is_finished") == true){

我控制了Nr。完成的匹配126是好的!

Log.e("id", String.valueOf(i));

我在数组中得到结果并对其进行排序:

Arrays.sort(addressArray, StandingsSort.ORDER_BY_RULES);
        //Print the sorted array
        for(int i=0; i<addressArray.length; i++){

            mylist[i] = new String(addressArray[i].toString());
        }

当我删除该行时,代码正常工作:(并切换到完整的saison,所有匹配都已完成.306匹配)

if (e.getBoolean("match_is_finished") == true){
然后我得到了我想要的一切。但是当我过滤结果时,Array.sort不起作用。为什么呢?

这里是json link:

  },
  "league_name": "1. Fussball-Bundesliga 2013/2014",
  "match_is_finished": false,
  "match_date_time_utc": "2014-05-10T13:30:00+00:00",
  "id_team2": "7",
  "goals": null,
  "league_saison": "2013",
  "match_results": null,
  "group_id": "8820",
  "icon_url_team1": "http://www.openligadb.de/images/teamicons/Hertha_BSC.gif",
  "league_shortcut": "bl1",
  "group_order_id": "34",
  "icon_url_team2": "http://www.openligadb.de/images/teamicons/Borussia_Dortmund.gif",
  "last_update": "2013-06-21T22:57:04+00:00",
  "match_id": "24171",
  "name_team1": "Hertha BSC",
  "group_name": "34. Spieltag",
  "points_team1": "-1",
  "match_date_time": "2014-05-10T15:30:00+00:00",
  "name_team2": "Borussia Dortmund"
}

for(int i=0;i<matchdata.length();i++){
                    JSONObject e = matchdata.getJSONObject(i);
                    //only read finished Matchs  "true".equals(e.getString("name"))
                    if (e.getBoolean("match_is_finished") == true){
                    //if (null != (e.getString("goals"))){
                    //calculate Points
                    int myVarGoal1 = Integer.valueOf(e.getString("points_team1"));
                    int myVarGoal2 = Integer.valueOf(e.getString("points_team2"));
                    if ((myVarGoal1) > (myVarGoal2)){
                        myPoint1 = 3;
                        myPoint2 = 0;
                    }
                    if ((myVarGoal1) < (myVarGoal2)){
                        myPoint1 = 0;
                        myPoint2 = 3;
                    }
                    if ((myVarGoal1) == (myVarGoal2)){
                        myPoint1 = 1;
                        myPoint2 = 1;
                    }
                    int calc1 = (map.get(e.getString("id_team1")) + myPoint1);
                    int calc2 = (map.get(e.getString("id_team2")) + myPoint2);
                    map.put("id",  Integer.valueOf(i));
                    map.put(e.getString("id_team1"), calc1);
                    map.put(e.getString("id_team2"), calc2);

                    //calculate Goals
                    int calcGoal1 = (map.get(e.getString("id_team1")+"G+") + myVarGoal1);
                    int calcGoal2 = (map.get(e.getString("id_team1")+"G-") + myVarGoal2);
                    int calcGoal3 = (map.get(e.getString("id_team2")+"G+") + myVarGoal2);
                    int calcGoal4 = (map.get(e.getString("id_team2")+"G-") + myVarGoal1);
                    map.put(e.getString("id_team1")+"G+", calcGoal1);
                    map.put(e.getString("id_team1")+"G-", calcGoal2);
                    map.put(e.getString("id_team2")+"G+", calcGoal3);
                    map.put(e.getString("id_team2")+"G-", calcGoal4);

                    Log.e("id", String.valueOf(i));
                    }

0 个答案:

没有答案