- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 JSON 是:
[
{
"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"
}
]
我希望它根据距离排序并删除重复的 stationCode。我尝试使用简单的 if else,但该过程会太多.. 任何专门用于排序的快捷方式。
最佳答案
我不久前写了这个实用程序,它对 JSONObjects
的 JSONArray
进行排序唯一的条件是你的 JSONobjects
必须包含你想要排序
的键
(如果你想排序,它也接受一组键基于几个键)
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<JSONObject> {
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());
}
}
现在如果你想删除重复项,你可以通过它们迭代
Set<String> stationCodes=new HashSet<String>();
JSONArray tempArray=new JSONArray();
for(int i=0;i<yourJSONArray.size();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");
关于java - 如何使用 Java 从 JSONArray 中删除重复对象并对其进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32371236/
我正在使用网络服务请求直方图数据。数据是数组中的一组数组: [[1375056000000,23.284713745117],[1375142400000,3.809531211853], [1375
这是我的 json 文件的相关部分: "categories": [ [ "Belgian Restaurant", "belgian" ],
我目前很困惑,为什么我不能在我的 Android 应用程序中从 JSONArray 中提取 JSONArray。下面给出了一个示例和我的源代码片段。 //The JSON { "cur
我正在尝试解析 JSONObject。 这个 JSONObject 中有一个 JSONArray,它在 JSONArray 中还有另一个 JSONArray。 我试图解析的 json 形式如下。 {
我正在尝试解析对象数据列表中的 JSON 数组 payment_details。 在这里,我已经成功解析了回收站 View 中调用的数据数组,但我无法调用 payment_details 数组。 适配
我正在寻找一个选项,可以将多个值添加到 JSONArray 并将其添加到另一个 JSONArray 中,而无需创建多个变量。例如: JSONArray array1 = new JSONArr
我遇到过一种情况,org.json.JSONArray 对象非常大,这最终会导致延迟和其他问题。因此,我们决定将 JSONArray 分割成更小的 block 。例如,如果 JSONArray 是这样
您好,我正在尝试在另一个 json 数组中解析一个 json 数组。帮我解决这个问题。 我的 Json 数组是 "results":[ { "percentcompleted"
我正在使用 Gson用于解析 json 响应。我需要在 JsonArray 中解析 JSONArry.. 我的回答是.. { "message": "Retreive sucessfully", "f
@GET @Produces("application/json") public Response GetAll() throws JSONException{ tblCategoryDao
我从服务器获取以下 json: { "Data": [ { "Record": [ " d11", "d12"
我正在使用 ajax 将 json 对象从 javascript 传递到 java servlet。 var jsonObj = JSON.stringify(objArray); //Then I
我需要获取作为“结果”内部数组的“示例”数据 JSON FILE 并将其附加到字符串。最简单的方法是什么? 这就是我得到“定义”的方式 private JSONObject queryResults;
我需要从 JSONArray 获取 JSONArray: JSONParser parser = new JSONParser(); JSONObject jObject=(J
我有一个 jsonArray 响应。我需要阅读此响应,其中包含 3 个 jsonArray。 这是 json 响应。这是一个 GET 请求并通过 Volley 请求发送。 [ "0x9000",
在此 Json 响应中,如何使用 Java 访问“smallImageUrls”并获取图像 url? 似乎“smallImageUrls”是“匹配”jsonarray 中的一个数组。如果我错了,请有人
这个问题在这里已经有了答案: Convert string to JSON array (10 个答案) 关闭 5 年前。 如何正确地将此字符串转换为 jsonArray? { "myArr
是否可以从较新的 EJB JSON 库转换为较旧的 org.json 库,而无需依赖 org.json.simple 或 GSON? 在下面的例子中,“buttons”是一个填充的 JsonArr
如何从jsonarray获取jsonarray? 我试过的代码。 for(loop) List stringList = new ArrayLis
我目前是 Json 新手,遇到了一个问题。我搜索了很多但找不到答案! 我从 json url 获取名称列表。名称可以在此 json 文件中重复,但我只想将它们的一条记录保留到我称为“arr”的新数组中
我是一名优秀的程序员,十分优秀!