簡體   English   中英

如何按字符串對JSONOBJECT排序?

[英]How to sort JSONOBJECT by a string?

我正在尋找一種解析json api來在Android中建立一個listview的方法,它運行良好,但是我無法按json的“ overall_league_position”對對象進行排序。

我已經看到了很多與此主題有關的問題,但是我不知道為什么我不能在我的代碼中實現Collections.sort 我以為是因為我的json以數組而不是對象開頭。

JSON格式

   [
    {
    "country_name": "France",
    "league_id": "129",
    "league_name": "National",
    "team_name": "Grenoble",
    "overall_league_position": "4",
    "overall_league_payed": "26",
...
    },
...
]

爪哇

 private void loadList() {
            final String JSON_URL = "*************************";
            //creating a string request to send request to the url
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            //hiding the progressbar after completion and showing list view
                            progressBar.setVisibility(View.GONE);
                            listView.setVisibility(View.VISIBLE);

                            // Showing json data in log monitor
                            Log.d("json", response.toString());

                            try {
                                //we have the array named hero inside the object
                                //so here we are getting that json array


                                //now looping through all the elements of the json array
                                for (int i = 0; i < response.length(); i++) {
                                    //getting the json object of the particular index inside the array
                                    JSONObject jsonObject = response.getJSONObject(i);

                                    //creating a hero object and giving them the values from json object
                                    Movie item = new Movie();


                                    item.setName(jsonObject.getString("team_name"));
                                    item.setPosition(jsonObject.getString("overall_league_position"));
                                    item.setPoints(jsonObject.getString("overall_league_PTS"));



                                    //adding the json data to list
                                    movieList.add(item);



Collections.sort(movieList, new Comparator<movieList>() {
    @Override
    public int compare(movieList lhs, movieList rhs) {
        return lhs.getPosition().compareTo(rhs.getPosition());
    }
});

                                }


                                //creating custom adapter object
                                classementadapter mAdapter = new classementadapter(movieList, getApplicationContext());

                                //adding the adapter to list view
                                listView.setAdapter(mAdapter);


                                mAdapter.notifyDataSetChanged();

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

                        }

                    },

非常感謝您的寶貴幫助,並教了我:) PS:我遵循了本教程: https : //www.androidlearning.com/android-json-parsing-using-volley/

在這里,您正在對字符串值進行排序,但我認為total_league_position值始終為整數,因此您的排序不起作用。

對於這種排序,您可以嘗試下面的代碼

Collections.sort(movieList, new Comparator<Movie>() {
            @Override
            public int compare(Movie lhs, Movie rhs) {
                return Integer.parseInt(lhs.getPosition()) - Integer.parseInt((rhs.getPosition()));
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM