java jsonarray 删除,如何使用Java从JSONArray删除重复对象和排序对象

博客内容介绍了如何对包含JSON对象的数组进行按特定字段排序,并移除重复的stationCode。提供了一个实用的Java工具类,实现了根据指定字段升序或降序排序的功能,并给出了去除重复stationCode的示例代码。
摘要由CSDN通过智能技术生成

My JSON is:

[

{

"distance":32,

"stationCode":"MIG",

"name":"Midghat",

"platforms":"2"

},

{

"distance":32,

"stationCode":"MIG",

"name":"Midghat",

"platforms":"2"

},

{

"distance":69,

"stationCode":"MDDP",

"name":"Mandideep",

"platforms":"2"

},

{

"distance":69,

"stationCode":"MDDP",

"name":"Mandideep",

"platforms":"2"

},

{

"distance":18,

"stationCode":"HBD",

"name":"Hoshangabad",

"platforms":"2"

},

{

"distance":18,

"stationCode":"HBD",

"name":"Hoshangabad",

"platforms":"2"

},

{

"distance":37,

"stationCode":"CHQ",

"name":"Choka",

"platforms":"2"

},

{

"distance":37,

"stationCode":"CHQ",

"name":"Choka",

"platforms":"2"

},

{

"distance":85,

"stationCode":"HBJ",

"name":"Habibganj",

"platforms":"5"

},

{

"distance":85,

"stationCode":"HBJ",

"name":"Habibganj",

"platforms":"5"

},

{

"distance":0,

"stationCode":"ET",

"name":"ItarsiJn",

"platforms":"28"

},

{

"distance":8,

"stationCode":"PRKD",

"name":"Powerkheda",

"platforms":"2"

},

{

"distance":8,

"stationCode":"PRKD",

"name":"Powerkheda",

"platforms":"2"

},

{

"distance":55,

"stationCode":"ODG",

"name":"ObaidullaGanj",

"platforms":"2"

},

{

"distance":55,

"stationCode":"ODG",

"name":"ObaidullaGanj",

"platforms":"2"

},

{

"distance":44,

"stationCode":"BKA",

"name":"Barkhera",

"platforms":"2"

},

{

"distance":44,

"stationCode":"BKA",

"name":"Barkhera",

"platforms":"2"

},

{

"distance":79,

"stationCode":"MSO",

"name":"Misrod",

"platforms":"2"

},

{

"distance":79,

"stationCode":"MSO",

"name":"Misrod",

"platforms":"2"

},

{

"distance":25,

"stationCode":"BNI",

"name":"Budni",

"platforms":"2"

},

{

"distance":25,

"stationCode":"BNI",

"name":"Budni",

"platforms":"2"

},

{

"distance":91,

"stationCode":"BPL",

"name":"BhopalJn",

"platforms":"6"

},

{

"distance":63,

"stationCode":"ITKL",

"name":"ItayaKalan",

"platforms":"2"

},

{

"distance":63,

"stationCode":"ITKL",

"name":"ItayaKalan",

"platforms":"2"

}

]

I want it to sort according to distance and remove duplicate stationCode. I tried using simple if else but that process will be too much.. any shortcut for same specially for sorting.

解决方案

I wrote this utility a while ago, it sorts a JSONArray of JSONObjects

Only condition is that your JSONobjects must contain the keys you want to sort based on (it also accept a set of keys if you want to sort based on several keys)

import java.util.Collections;

import java.util.Comparator;

import java.util.Random;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class JSONArraySort {

@SuppressWarnings("unchecked")

public static void sortASCE(JSONArray array, Object key) {

Object[] keys = { key };

Collections.sort(array, new JSONArrayComparator(false, keys));

}

@SuppressWarnings("unchecked")

public static void sortDESC(JSONArray array, Object key) {

Object[] keys = { key };

Collections.sort(array, new JSONArrayComparator(true, keys));

}

@SuppressWarnings("unchecked")

public static void sortASCE(JSONArray array, Object[] key) {

Collections.sort(array, new JSONArrayComparator(false, key));

}

@SuppressWarnings("unchecked")

public static void sortDESC(JSONArray array, Object[] key) {

Collections.sort(array, new JSONArrayComparator(true, key));

}

private static class JSONArrayComparator implements Comparator {

private final Object[] KEYS;

private final boolean DESC;

public JSONArrayComparator(boolean DESC, Object[] KEYS) {

this.KEYS = KEYS;

this.DESC = DESC;

}

@Override

public int compare(JSONObject object1, JSONObject object2) {

int length = KEYS.length;

for(int i = 0 ; i < length ; i++){

String KEY = KEYS[i].toString();

Object one = object1.get(KEY);

Object two = object2.get(KEY);

if(Number.class.isAssignableFrom(one.getClass()) && Number.class.isAssignableFrom(two.getClass())){

Double numOne = Number.class.cast(one).doubleValue();

Double numTwo = Number.class.cast(two).doubleValue();

int compared = 0;

if(DESC){

compared = numTwo.compareTo(numOne);

}else{

compared = numOne.compareTo(numTwo);

}

if(i == KEYS.length - 1 || compared != 0){

return compared;

}

}else{

int compared = 0;

if(DESC){

compared = two.toString().compareTo(one.toString());

}else{

compared = one.toString().compareTo(two.toString());

}

if(i == KEYS.length - 1 || compared != 0){

return compared;

}

}

}

// this shouldn't happen.

return 0;

}

}

//testing...

public static void main(String... args) {

JSONArray array1 = new JSONArray();

for(int i = 0 ; i < 100 ; i++){

Random random = new Random();

int num1 = random.nextInt(10);

int num2 = random.nextInt(10);

JSONObject object = new JSONObject();

object.put("num1", num1);

object.put("num2", num2);

array1.add(object);

}

String[] keys = { "num1", "num2" };

sortASCE(array1, keys);

System.out.println(array1.toString());

}

}

now if you want to remove duplicates you can iterate through them

Set stationCodes=new HashSet();

JSONArray tempArray=new JSONArray();

for(int i=0;i

String stationCode=yourJSONArray.getJSONObject(i).getString("stationCode");

if(stationsCodes.contains(stationCode){

continue;

}

else{

stationsCodes.add(stationCode);

tempArray.add(yourJSONArray.getJSONObject(i));

}

}

yourJSONArray= tempArray; //assign temp to original

//here how you can sort it using utility above:

JSONArraySort.sortASCE(yourJSONArray,"distance");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值