Java:将两个json对象与主键合并在一起 [英] Java: Merging two json objects together with primary key

查看:226
本文介绍了Java:将两个json对象与主键合并在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在内存中有两个JSONObject数组,每个对象在两个数组中都有一个类似的键:

Lets say I have two arrays of JSONObjects in memory and each object has a key that is similar in both arrays:

数组1

[
  {
    "name": "Big Melons Co.",
    "location": "Inner City Dubai"
    "id": "1A"
  },
  {
    "name": "Pear Flavored Juices Ltd",
    "location": "Seychelles"
    "id": "2A"
  },
  {
    "name": "Squeeze My Lemons LLC",
    "location": "UK"
    "id": "3A"
  }, {other JSON Objects...} ]

数组2

[
  {
    "acceptsCard": "true"
    "id": "1A"
  },
  {
    "acceptsCard": "false"
    "id": "2A"
  },
  {
    "acceptsCard": "false"
    "id": "3A"
  }, {other JSON Objects...} ]

现在,我想根据id的主键将两个数组合并在一起他们是beco我在我的服务器端然后将结果发送回我的前端 - 由此产生的对象的arraylist应如下所示:

Now, I want to merge the two arrays together based on the primary key of "id" so they become one on my server side and then send the results back to my frontend - the resulting arraylist of objects should look like this:

已合并的阵列(结果)

  [
      {
        "name": "Great Juice Co.",
        "location": "Inner City Dubai"
        "acceptsCard": "true"
        "id": "1A"
      },
      {
        "name": "Pear Flavored Juices Ltd",
        "location": "Seychelles"
        "acceptsCard": "false"
        "id": "2A"
      },
      {
        "name": "Squeeze My Lemons LLC",
        "location": "UK"
        "acceptsCard": "false"
        "id": "3A"
      }, {other JSON Objects...} ]

我该怎么做有效吗?

我可以想到一种非常低效的方法(我很害怕实现这个) - 我会循环遍历数组1或2中的每个项目使用equal()我thod为id字段中的字符串,以查看两者是否匹配。如果匹配,我会创建一个新的JSONObject来包含数组1和2中的字段。

I can think of one highly inefficient way to do this (I'm dreading implementing this) - I would loop though each item in either array 1 or 2 and use the equal() method for the string in the "id" field to see whether the two matches. If they match, I would create a new JSONObject to contain both the fields from array 1 and 2.

推荐答案

我的Java是一个有点生锈,但我会使用地图。

My Java is a little rusty but I would use a map.

List<JSONObject> objectsA = ... ;
List<JSONObject> objectsB = ... ;

Map entries = new HashMap<String, JSONObject>();
List<JSONObject> allObjects = new ArrayList<JSONObject>();
allObjects.addAll(objectsA);
allObjects.addAll(objectsB);

for (JSONObject obj: allObjects) {
    String key = obj.getString("id");
    JSONObject existing = entries.get(key);
    if (existing == null) {
        existing = new JSONObject();
        entries.put(key, existing);
    }

    for (String subKey : obj.keys()) {
        existing.put(subKey, obj.get(subKey));
    }
}

List<JSONObject> merged = entries.values();

这比两个嵌套循环更有效,而且还有改进的余地。

This is more efficient than two nested loops and there's still room for improvement.

编辑:参考外部文件和相关答案。

References to external documentation and related answers.

  • http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
  • http://www.json.org/javadoc/org/json/JSONObject.html
  • https://stackoverflow.com/a/2403427/937006

这篇关于Java:将两个json对象与主键合并在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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