Android:通过休息呼叫获得或获取的JSON数据的排序

时间:2017-02-08 10:35:58

标签: android

我的问题是我必须显示从JSON文件中获取的数据,json文件位于下方。但是我希望数据在从JSON文件获取之后处于排序形式,即在从viewmodel中的控制器获取数据的其余调用之后,以便我可以直接在View类上显示它。有cardviews。

{
 "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: ");

1 个答案:

答案 0 :(得分:0)

解决方案是使用JSONArray“employeeList”将其转换为JSONObject的ArrayList并使用集合对其进行排序。

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

相关问题