Java Android对Json数据进行排序

Java Android对Json数据进行排序,java,android,json,Java,Android,Json,我是新来的。我试图对从json解析中获得的数据进行排序,但没有任何效果。 甚至一个错误都没有。 我试着 我不知道我的代码出了什么问题,因为没有错误。 但是有一个警告,默认语言环境是bug的常见来源:在这里使用ToLowerCaseScale return (lhs.getString("item_name").toLowerCase().compareTo(rhs.getString("item_name").toLowerCase())); 有人能帮我解决这个问题吗 这是我的java代码

我是新来的。我试图对从json解析中获得的数据进行排序,但没有任何效果。 甚至一个错误都没有。 我试着

我不知道我的代码出了什么问题,因为没有错误。 但是有一个警告,默认语言环境是bug的常见来源:在这里使用ToLowerCaseScale

return (lhs.getString("item_name").toLowerCase().compareTo(rhs.getString("item_name").toLowerCase()));
有人能帮我解决这个问题吗

这是我的java代码

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                ArrayList<JSONObject> array = new ArrayList<JSONObject>();
                JSONArray jsonArray = new JSONArray();

                // looping through All Contacts
                for (int i = 0; i < foods.length(); i++) {
                    try {
                    JSONObject c = foods.getJSONObject(i);

                    if(c.getString("category_id").equals("1")) {
                        // Adding to array. Only items with category_id = 1 will be sorted.
                        array.add(foods.getJSONObject(i));  // This line is important

                        String item_id = c.getString(TAG_ITEM_ID);

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ITEM_ID, item_id);  

                        // adding contact to contact list
                        foodslist.add(contact);

                    }
            } catch (JSONException e) {
                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(Locale.getDefault()).compareTo(rhs.getString("Name").toLowerCase(Locale.getDefault())));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            return 0;
                        }
                    }
                });
            }
          }      
        else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

谢谢。这只是一个警告

如果您觉得不舒服,您可以使用@SuppressWarnings来避免它,前提是您的应用程序不适用于多种语言 或

您可以使用:toLowerCelocale.getDefault;以避免此警告。确保导入java.util.Locale

编辑

希望下面的代码能够帮助您:

ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
    try {
     JSONObject c = jsonArray.getJSONObject(i);
    if(c.getString("category_id").equals("1")
    {
         // Adding to array. Only items with category_id = 1 will be sorted.
         array.add(jsonArray.getJSONObject(i));  // This line is important

         // All other stuff here.....
          String item_id = c.getString(TAG_ITEM_ID);
                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ITEM_ID, item_id);
                        // adding contact to contact list
                        foodslist.add(contact);
    }


    } 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(Locale.getDefault()).compareTo(rhs.getString("Name").toLowerCase(Locale.getDefault())));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }
});
重新编辑

而不是使用

.toLowerCase().
像这样改变它

.toLowerCase(Locale.getDefault())
这会有帮助的


还要检查变量中是否为Null,检查数组是否为>0。

这只是一个警告。如果你觉得不舒服,你可以使用@suppresswarnings来避免它,前提是你的应用程序不是针对多语言的,但是你知道为什么排序不起作用吗?这是因为你的代码与你尝试使用的源代码不同。您已经声明了数组,但没有在代码中的任何地方为其赋值;这是排序所必需的。在您的代码中,这一行丢失。我正在尝试执行您所说的操作,但有一个错误数组无法解析为变量。很抱歉,我忘记在我的代码:D上添加add try{,但请求我添加finally以完成blockstatements。
.toLowerCase().
.toLowerCase(Locale.getDefault())