JAVA,当某个json数据中一个字段与另一个json数据中的字段值相同时,对两个json进行合并且相加,

原JSON串:[{"depid":"5","score":"10"},{"depid":"4","score":"40"},{"depid":"4","score":"30"},"depid":"5","score":"30"}]
结果::[{"depid":"5","score":"40"},{"depid":"4","score":"70"}],
具体的java代码做参考。谢谢!

要判断json数据的字段与其他数据是否相同,那么肯定是要先解析json数据。解析json数据的方式很多,Gson、FastJson都是很简便实用的工具。这里以Gson为例。

import java.lang.reflect.Type;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Divination {
    public static void main(String[] args) {
        String jsonStr = "[{\"depid\":\"5\",\"score\":\"10\"},{\"depid\":\"4\",\"score\":\"40\"},{\"depid\":\"4\",\"score\":\"30\"},{\"depid\":\"5\",\"score\":\"30\"}]";
        System.out.println("原始的json字符串:" + jsonStr);

        // 解析
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<JsonData>>() {
        }.getType();
        ArrayList<JsonData> list = gson.fromJson(jsonStr, type);

        // 合并
        List<JsonData> ordered = new ArrayList<>();
        Map<Integer, JsonData> map = new HashMap<>();
        for (JsonData jsonData : list) {
            JsonData data = map.get(jsonData.getDepid());
            if (data != null) { // depid相同的合并score字段
                data.setScore(data.getScore() + jsonData.getScore());
            } else {
                map.put(jsonData.getDepid(), jsonData);
                ordered.add(jsonData);
            }
        }

        // 还原为json字符串
        System.out.println("合并后json字符串:" + gson.toJson(map.values()));
        System.out.println("合并后json字符串(按原来的顺序):" + gson.toJson(ordered));
    }
}

class JsonData {
    private int depid;
    private int score;

    public int getDepid() {
        return depid;
    }

    public void setDepid(int depid) {
        this.depid = depid;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

以上代码中List<JsonData> ordered = new ArrayList<>();是为了按原json数组的顺序输出合并后的结果,如果对顺序无要求可以删除相关代码。

运行结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-08-19
将json转化为list 然后遍历list中的map 得到新的list 在转成json
第2个回答  推荐于2018-02-06
String str = "[{\"score\": \"10\",\"depid\": \"5\"},{\"score\": \"40\",\"depid\": \"4\"},{\"score\": \"30\",\"depid\": \"4\"},{\"score\": \"30\",\"depid\": \"5\"}]";
JSONArray array = (JSONArray) JSONSerializer.toJSON(str);
HashMap<String,String> map = new HashMap<String,String>();
for(int i = 0; i < array.length; i++){
    JSONObject obj = array[i];
    String depid = obj.getString("depid");
    String score = obj.getString("score");
    String s1 = map.get(depid);
    if(null == s1){
        map.put(depid, score);
    }else{
        int s = Interger.parseInt(s1);
        map.put(depid, score + s);
    }
}

foreach map for convert to JSON again

本回答被网友采纳
第3个回答  2015-08-19
这只能通过遍历原来的数组,然后再处理啊。
第4个回答  推荐于2016-11-22
public static void main(String[] args)
    {
        String string = "[{\"depid\":\"5\",\"score\":\"10\"},{\"depid\":\"4\",\"score\":\"40\"},{\"depid\":\"4\",\"score\":\"30\"},{\"depid\":\"5\",\"score\":\"30\"}]";
        JSONArray fromObject = JSONArray.fromObject(string);
        Map<String,Integer> map = new HashMap<String, Integer>();
       
        for (Object object : fromObject)
        {
            JSONObject jsonObject = (JSONObject) object;
            String depid = (String)jsonObject.get("depid");
            Integer score = Integer.valueOf((String)jsonObject.get("score"));
            if (map.containsKey(depid))
            {
                int integer = map.get(depid);
                map.put(depid, integer+score);
            }
            else
            {
                map.put(depid, score);
            }
        }
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        JSONArray jsonArray = new JSONArray();
        
        for (Entry<String, Integer> entry : entrySet)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("depid",entry.getKey());
            jsonObject.put("score",String.valueOf(entry.getValue()));
            jsonArray.add(jsonObject);
        }
        System.out.println(jsonArray.toString());
    }

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网