的Andr​​oid如何排序JSONObjects的JSONArray [英] Android how to sort JSONArray of JSONObjects

查看:228
本文介绍了的Andr​​oid如何排序JSONObjects的JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做jsonobjects的jsonarray。现在我需要的JSONArray排序从jsonobjects值的基础。以前我整理自定义对象的ArrayList是这样的:

I have made a jsonarray of jsonobjects. Now I need to sort the JSONArray on base of a value from the jsonobjects. Formerly I sorted ArrayLists of custom objects like this:

比较:

public class KreeftenComparatorLA implements Comparator<Kreeft> {
    public int compare(Kreeft left, Kreeft right) {
        return left.latijnseNaam.compareTo(right.latijnseNaam);
    }
}
public class KreeftenComparatorNL implements Comparator<Kreeft> {
    public int compare(Kreeft left, Kreeft right) {
        return left.naam.compareTo(right.naam);
    }
}

然后排序的数组列表:

And then sort the arraylist:

Collections.sort(db.lijst, new KreeftenComparatorLA());

Collections.sort(db.lijst, new KreeftenComparatorNL());

但是,当我尝试同样的事情,与JSONArray像这样(JA =我JSONArray),其中

But when I try the same thing with the JSONArray like this (JA = my jsonarray)

Collections.sort(JA, new KreeftenComparatorNL());

在Collections.sort提供了一个错误:

the Collections.sort gives an error:

在类型集合的方法排序(名单,比较器)是不适用的参数(JSONArray,ThisActicity.KreeftenComparatorNL)

The method sort(List, Comparator) in the type Collections is not applicable for the arguments (JSONArray, ThisActicity.KreeftenComparatorNL)

有谁知道如何在JSONArray排序?

推荐答案

问题是,JSONArray或多或少持有JSONObjects(和其他JSONArrays)最终都是字符串。完全反序列化字符串转换成的POJO,整理这些,再放回JSON是相当沉重的。

The issue is that JSONArray more or less holds JSONObjects (and other JSONArrays) which ultimately are strings. Deserializing the strings entirely into POJOs, sorting those, then back into JSON is fairly heavy.

第二个问题是,一个JSONArray可以包括:布尔,JSONArray,JSONObject的,数字,字符串或JSONObject.NULL对象;也就是说,它是混合类型,很难只转储元素融入到某种类型和排序的,然后通过列表倾倒分拣物品放回JSON数组的列表。只有特定的方式来获得从JSONArray每个元素的常见类型是使用对象获取过程中的()方法..然后你必须是Object对象,将不能够做任何有意义的排序上他们没有重新审查序列化的问题。

The second issue is that a JSONArray can contain: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object; i.e. it is mixed types, making it hard to just dump the elements into a List of some type and sort that, then pass through the list dumping sorted items back into the JSON array. the only certain way to get a common type of each element from the JSONArray is using the Object get() method.. of course then all you have is Object objects and won't be able to do any meaningful sorting on them without revisiting the serialization issue.

假设你的JSONArray包含均匀结构化值,你可以遍历JSONArray,调用类型的get()的一个对每一个方法,直接排放到一个列表类型,然后在该排序。如果你的JSONArray刚刚举办的简单的类型(如String)或数字,这是比较容易的。这是不准确code,但这样的:

Assuming your JSONArray contains homogeneously structured values, you could iterate through the JSONArray, calling one of the typed get() methods on each one, dumping them into a List type, then sorting on that. If your JSONArray just holds "simple" type like String or numbers, this is relatively easy. This isn't exact code but something like:

List<String> jsonValues = new ArrayList<String>();
for (int i = 0; i < myJsonArray.length(); i++)
   jsonValues.add(myJsonArray.getString(i));
Collections.sort(jsonValues);
JSONArray sortedJsonArray = new JSONArray(jsonValues);

当然,如果你有嵌套对象,这可能会变得有些棘手。如果您想对住在顶层的排序值(S),也未必洙坏...

Of course, if you have nested objects this can get a little trickier. If the value(s) you want to sort on live in the top level, it may not be soo bad...

List<JSONObject> jsonValues = new ArrayList<JSONObject>();
for (int i = 0; i < myJsonArray.length(); i++)
   jsonValues.add(myJsonArray.getJSONObject(i));

然后用一个比较像这样的排序:

Then use a comparator like this to sort:

class JSONComparator implements Comparator<JSONObject>
{

    public int compare(JSONObject a, JSONObject b)
    {
        //valA and valB could be any simple type, such as number, string, whatever
        String valA = a.get("keyOfValueToSortBy");
        String valB = b.get("keyOfValueToSortBy");

        return valA.compareTo(valB);
        //if your value is numeric:
        //if(valA > valB)
        //    return 1;
        //if(valA < valB)
        //    return -1;
        //return 0;    
    }
}

再次这使得有关在JSONArray数据的同质一些假设。调整你的情况下,如果可能的。你也将需要添加异常处理等编码快乐!

Again, this makes some assumptions about the homogeneity of the data in your JSONArray. Adjust to your case if possible. Also you will need to add your exception handling, etc. Happy coding!

修改 固定基于注释

这篇关于的Andr​​oid如何排序JSONObjects的JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆