【问题标题】:Compare two json arrays and return common elements java比较两个json数组并返回共同元素java
【发布时间】:2020-06-04 09:05:34
【问题描述】:

我是java 的新手。我有一个像这样的JSONArray

String data1 =  "[{\"_index\":\"sales_csv\",\"_source\":{\"Order Date\":\"2016-01-28T18:37:00.000Z\"},\"sort\":[1576653361740],\"_score\":null},{\"_index\":\"sales_csv\",\"_source\":{\"Order Date\":\"2016-01-29T18:35:00.000Z\"},\"sort\":[1576653361740],\"_score\":null},{\"_index\":\"sales_csv\",\"_source\":{\"Order Date\":\"2016-01-30T18:41:00.000Z\"},\"sort\":[1576653361739],\"_score\":null}]";
String data2 = "[ { \"_index\": \"sales_csv\", \"_source\": { \"Order Date\": \"2016-01-28T18:37:00.000Z\" }, \"sort\": [ 1576653361740 ], \"_score\": null }, { \"_index\": \"sales_csv\", \"_source\": { \"Order Date\": \"2016-01-29T18:35:00.000Z\" }, \"sort\": [ 1576653361740 ], \"_score\": null }, { \"_index\": \"sales_csv\", \"_source\": { \"Order Date\": \"2016-01-17T18:35:00.000Z\" }, \"sort\": [ 1576653361736 ], \"_score\": null } ]" ;

JSONArray jsonElement1 = new JSONArray(data1) ;
JSONArray jsonElement2 = new JSONArray(data2) ;

转换后:

jsonElement1 = [
    {"_index":"sales_csv","_source":{"Order Date":"2016-01-28T18:37:00.000Z"},"sort":[1576653361740],"_score":null},
    {"_index":"sales_csv","_source":{"Order Date":"2016-01-29T18:35:00.000Z"},"sort":[1576653361740],"_score":null},
    {"_index":"sales_csv","_source":{"Order Date":"2016-01-30T18:41:00.000Z"},"sort":[1576653361739],"_score":null}]
jsonElement2 = [ 
    { "_index": "sales_csv", "_source": { "Order Date": "2016-01-28T18:37:00.000Z" }, "sort": [ 1576653361740 ], "_score": null },  
    { "_index": "sales_csv", "_source": { "Order Date": "2016-01-17T18:35:00.000Z" }, "sort": [ 1576653361736 ], "_score": null },
    { "_index": "sales_csv", "_source": { "Order Date": "2016-01-29T18:35:00.000Z" }, "sort": [ 1576653361740 ], "_score": null }, ]

我需要比较两个 JSONArray 并返回两个数组之间的公共 JSONobjects 并将其附加到新的 JSONarray output

所需输出:

output = [ 
    { "_index": "sales_csv", "_source": { "Order Date": "2016-01-28T18:37:00.000Z" }, "sort": [ 1576653361740 ], "_score": null }, 
    { "_index": "sales_csv", "_source": { "Order Date": "2016-01-29T18:35:00.000Z" }, "sort": [ 1576653361740 ], "_score": null }
     ]

我试过这个答案Comparing 2 JSONArray。但它返回真/假。

这是我尝试过的代码:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.*;

import static java.lang.System.out;

public class home {
    public static JSONArray sortJsonArray(JSONArray inputUnSort, JSONArray outputSort) throws JsonProcessingException {
        for (int i = 0; i < inputUnSort.length(); i++) {
            ObjectMapper om = new ObjectMapper();
            om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
            Map<String, Object> map = null;
            try {
                map = om.readValue(inputUnSort.getJSONObject(i).toString(), HashMap.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String jsonstr = om.writeValueAsString(map);
            JSONObject json = new JSONObject(jsonstr);
            outputSort.put(json);
        }
        return outputSort;
    }
    public static JSONArray compareJsonArray(JSONArray jsonElement1, JSONArray jsonElement2) throws JsonProcessingException {
//        Sorting
        JSONArray jsonElement1sort = new JSONArray() ;
        JSONArray jsonElement2sort = new JSONArray();
        jsonElement1sort = sortJsonArray( jsonElement1,  jsonElement1sort);
        jsonElement2sort = sortJsonArray( jsonElement2,  jsonElement2sort);
        JSONArray outputJSONelement = new JSONArray();
        for (int i = 0; i < jsonElement1sort.length(); i++){
            for (int j = 0; j < jsonElement2sort.length(); j++) {
                if (jsonElement1sort.getJSONObject(i).toString().equals(jsonElement2sort.getJSONObject(j).toString())){
                    outputJSONelement.put(jsonElement1sort.getJSONObject(i));
                }
            }
        }
        return  outputJSONelement;
    }
    public static void main(String[] args) throws JsonProcessingException {
        String data1 =  "[{\"_index\":\"sales_csv\",\"_source\":{\"Order Date\":\"2016-01-28T18:37:00.000Z\"},\"sort\":[1576653361740],\"_score\":null},{\"_index\":\"sales_csv\",\"_source\":{\"Order Date\":\"2016-01-29T18:35:00.000Z\"},\"sort\":[1576653361740],\"_score\":null},{\"_index\":\"sales_csv\",\"_source\":{\"Order Date\":\"2016-01-30T18:41:00.000Z\"},\"sort\":[1576653361739],\"_score\":null}]";
        String data2 = "[ { \"_index\": \"sales_csv\", \"_source\": { \"Order Date\": \"2016-01-28T18:37:00.000Z\" }, \"sort\": [ 1576653361740 ], \"_score\": null }, { \"_index\": \"sales_csv\", \"_source\": { \"Order Date\": \"2016-01-29T18:35:00.000Z\" }, \"sort\": [ 1576653361740 ], \"_score\": null }, { \"_index\": \"sales_csv\", \"_source\": { \"Order Date\": \"2016-01-17T18:35:00.000Z\" }, \"sort\": [ 1576653361736 ], \"_score\": null } ]" ;
        JSONArray jsonElement1 = new JSONArray(data1) ;
        JSONArray jsonElement2 = new JSONArray(data2) ;
        out.println(jsonElement1);
        out.println(jsonElement2);
        out.println(compareJsonArray(jsonElement1, jsonElement2));
    }
}

上面的代码看起来很大。 实现这一目标的任何最短方法

【问题讨论】:

  • Setintersect方法怎么样?
  • 我没用过。你能用代码告诉我吗?我希望代码最小化和优化。来自python,python中有很多库可以让它变得简单。但是在java中,我找不到任何人。
  • 无论如何你都在做 (n^2) 比较,你可以得到只是删除排序
  • 我使用排序来排序 JSON 对象。有时,数据会出现乱序。所以对于那种情况,我使用了排序
  • 顺序无所谓,找到常用项。

标签: java arrays json for-loop


【解决方案1】:

由于您已经在进行 O(N^2) 比较以找到公共元素,因此您不需要之前对数组进行排序。仅删除排序也应该可以工作并减少大量冗余代码

使用JSONObject.similar()代替字符串比较

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2015-12-16
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多