本文介绍了如何从 Android 中的 JSON 对象对 JSON 数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 JSON 数组,我想根据名称和字母顺序对其进行排序.我的Json如下.
[{用户 ID":77,"InvID": 333,"电子邮件": "[email protected]",电话": "","名称": "峰会",公司": "",日期":2014-01-14T00:00:00"},{用户ID":0,"InvID": 334,"邮箱": "[email protected]",电话": "","姓名": "阿米特",公司": "",日期":2014-01-14T00:00:00"},{用户ID":3,"InvID": 335,"Email": "[email protected]",电话": "","Name": "Amitesh",公司": "",日期":2014-01-14T00:00:00"},{用户ID":0,"InvID": 336,"邮箱": "[email protected]",电话": "","名称": "FOO",公司": "",日期":2014-01-14T00:00:00"},{用户ID":0,"InvID": 337,"电子邮件": "[email protected]",电话": "","名称": "疯狂",公司": "",日期":2014-01-14T00:00:00"},{用户ID":1,"InvID": 338,"电子邮件": "[email protected]",电话": "","名称": "测试",公司": "",日期":2014-01-14T00:00:00"},{用户ID":0,"InvID": 339,"电子邮件": "[email protected]",电话": "",姓名": " ",公司": "",日期":2014-01-14T00:00:00"},{用户ID":0,"InvID": 340,"电子邮件": "[email protected]",电话": "","名称": "SAM",公司": "",日期":2014-01-14T00:00:00"},{用户ID":101,"InvID": 343,"电子邮件": "[email protected]",电话": "","名称": "阿努拉格",公司": "",日期":2014-01-14T00:00:00"},{用户ID":95,"InvID": 379,"电子邮件": "[email protected]",电话": "","名称": "S",公司": "",日期":2014-01-14T00:00:00"},{用户ID":0,"InvID": 428,"电子邮件": "[email protected]",电话": "","名称": "峰会",公司": "",日期":2014-01-16T00:00:00"},{用户ID":4,"InvID": 494,"电子邮件": "[email protected]",电话": "","Name": "Sameer P",公司": "",日期":2014-01-21T00:00:00"},{用户 ID":85,"InvID": 507,"电子邮件": "[email protected]","电话": "9404988188","Name": "Jonny Dep","公司": "Iconnect","日期": "2014-01-22T00:00:00"}]如您所见,我有一个键值名称",因此我获得了受人尊敬的名称.我想完全根据这个名称和字母顺序对这个 Json 数组进行排序.提前致谢.
解决方案
将 JSONArray
转换为 Arraylist
并使用 Collections 对你的 arraylist 进行排序
例如:
ArrayList数组 = 新的 ArrayList();JSONArray jsonArray = new JSONArray();for (int i = 0; i < jsonArray.length(); i++) {尝试 {array.add(jsonArray.getJSONObject(i));} catch (JSONException e) {//TODO 自动生成的 catch 块e.printStackTrace();}}Collections.sort(array, new Comparator() {@覆盖公共 int 比较(JSONObject lhs,JSONObject rhs){//TODO 自动生成的方法存根尝试 {return (lhs.getString("Name").toLowerCase().compareTo(rhs.getString("Name").toLowerCase()));} catch (JSONException e) {//TODO 自动生成的 catch 块e.printStackTrace();返回0;}}});
I have a JSON array, and I want to sort it Depending on the name and too alphabetically.My Json is as follows.
[
{
"UserID": 77,
"InvID": 333,
"Email": "[email protected]",
"Phone": "",
"Name": "Summet",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 0,
"InvID": 334,
"Email": "[email protected]",
"Phone": "",
"Name": "Amit",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 3,
"InvID": 335,
"Email": "[email protected]",
"Phone": "",
"Name": "Amitesh",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 0,
"InvID": 336,
"Email": "[email protected]",
"Phone": "",
"Name": "FOO",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 0,
"InvID": 337,
"Email": "[email protected]",
"Phone": "",
"Name": "Krazy",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 1,
"InvID": 338,
"Email": "[email protected]",
"Phone": "",
"Name": "Test",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 0,
"InvID": 339,
"Email": "[email protected]",
"Phone": "",
"Name": " ",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 0,
"InvID": 340,
"Email": "[email protected]",
"Phone": "",
"Name": "SAM",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 101,
"InvID": 343,
"Email": "[email protected]",
"Phone": "",
"Name": "Anurag",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 95,
"InvID": 379,
"Email": "[email protected]",
"Phone": "",
"Name": "S",
"Company": "",
"Date": "2014-01-14T00:00:00"
},
{
"UserID": 0,
"InvID": 428,
"Email": "[email protected]",
"Phone": "",
"Name": "Summet",
"Company": "",
"Date": "2014-01-16T00:00:00"
},
{
"UserID": 4,
"InvID": 494,
"Email": "[email protected]",
"Phone": "",
"Name": "Sameer P",
"Company": "",
"Date": "2014-01-21T00:00:00"
},
{
"UserID": 85,
"InvID": 507,
"Email": "[email protected]",
"Phone": "9404988188",
"Name": "Jonny Dep",
"Company": "Iconnect",
"Date": "2014-01-22T00:00:00"
}
]
As you can see I have a key value "Name" and in that I get respected names. I want to sort this Json array completely based on this names and that too alphabetically.Thanks in advance.
解决方案
Convert JSONArray
to Arraylist<JSONBbject>
and use Collections to sort your arraylist
for example :
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;
}
}
});
这篇关于如何从 Android 中的 JSON 对象对 JSON 数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!