【问题标题】:Android : Sorting of JSON Data obtained or taken by making rest callAndroid : 对通过 rest 调用获取或获取的 JSON 数据进行排序
【发布时间】:2017-02-08 10:35:58
【问题描述】:

我的问题是我必须显示从 JSON 文件中获取的数据,json 文件在下面。但是我希望数据在从 JSON 文件中获取后以排序形式出现,即在从 viewmodel 中的控制器获取数据时的 rest 调用之后,以便我可以直接在 View Class 上显示它。有卡片浏览量。

{
 "employeeList":[
    {
      "employeeName": "aaaa",
      "employeeStatus": "Trainee",
      "company": "IBM",
      "mobile": "894996662"
   },
   {
      "employeeName": "bbbb",
      "employeeStatus": "Fellowship",
      "company": "Wipro",
      "mobile": "9876000021"
   },
   {
      "employeeName": "cccc",
      "employeeStatus": "Fellowship",
      "company": "CGI",
      "mobile": "9876000021"
   },
   {
      "employeeName": "cccc",
      "employeeStatus": "Fellowship",
      "company": "CGI",
      "mobile": "9876000021"
   }
  ]
}

.java文件在下面,我是不是没有做排序。我需要帮助,谢谢。

try {
                    //As the data in rest call contained in JSONObject inside that JSONArray and inside
                    //JSONArray JSONObject is there ,Hence to get the data and set to the Model Class
                    //Creating JSON Object and obtained the data in bytes
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    Log.i("json object", "employeeList: ");
                    if (jsonObject != null) {
                        JSONArray jsonArray = jsonObject.getJSONArray("employeeList");
                        Log.i("json Array", "employeeList: " + jsonArray.length());
                        //Creating the object of modelClass

                        if (jsonArray.length() != 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject childObject = jsonArray.getJSONObject(i);
                                //Through the object of model Class the obtained data is set to the
                                //Model class
                                EnggFragModel enggFragModel = new EnggFragModel();

                                enggFragModel.setImageurl(childObject.getString("imageUrl"));
                                Log.i("image", "employeeData: "+childObject.getString("imageUrl"));
                                enggFragModel.setEmployeeName(childObject.getString("employeeName"));
                                enggFragModel.setEmployeeStatus(childObject.getString("employeeStatus"));
                                enggFragModel.setCompany(childObject.getString("company"));
                                enggFragModel.setEmployeeMobile(childObject.getString("mobile"));
                                enggFragModel.setEmployeeEmail(childObject.getString("emailId"));
                                enggFragModel.setEngineerID(childObject.getString("engineerId"));
                                Log.i("EngineerId", "employeeData: " + childObject.getString("engineerId"));
                                enggArrayList.add(enggFragModel);
                            }
                            enggViewModelInterface.enggViewMInterface(enggArrayList);
                            Log.i("Employee", "employeeList: " + enggArrayList);
                        }

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.i("Employee", "employeeList: ");

【问题讨论】:

标签: android


【解决方案1】:

解决方法是把 JSONArray "employeeList" 转换成 JSONObject 的 ArrayList 并排序,使用 Collections

ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
    try {
        array.add(jsonArray.getJSONObject(i));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Collections.sort(array, new Comparator<JSONObject>() {

    @Override
    public int compare(JSONObject lhs, JSONObject rhs) {
        // TODO Auto-generated method stub

        try {
            return (lhs.getString("Name").toLowerCase().compareTo(rhs.getString("Name").toLowerCase()));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }
});

Here is original

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2020-02-04
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多