合并JSON文件

时间:2015-07-15 18:56:47

标签: json

有人知道合并2个JSON文件的最佳方法吗?我希望推动“奖励表格”#39;从文件1到文件2,同时保持相同的数字'。考虑手动工作,如果只是少数,但我只处理超过1k条目。任何方向或帮助将不胜感激!

文件1包含:

{
    "Notices": [
        {
            "awardClaimForms": "form-21",
            "number": "2015-031"
        },
        {
            "awardClaimForms": "form22",
            "number": "2015-030"
        },
    ]
}

文件2包含:

{
    "Notices": [
        {
            "number": "2015-031",
            "link": "http://www.yahoo.com",
            "title": "myother sample title",
            "awardClaimDueDate": "March 01, 2013",
            "datePosted": "12/01/2012"
        },
        {
            "number": "2015-030",
            "link": "http://www.google.com",
            "title": "my sample title",
            "awardClaimDueDate": "March 01, 2013",
            "datePosted": "12/01/2012"
        },
    ]
}

期望的结果:

{
    "Notices": [
        {
            "number": "2015-031",
            "link": "http://www.yahoo.com",
            "title": "myother sample title",
            "awardClaimDueDate": "March 01, 2013",
            "awardClaimForms": "form-21",
            "datePosted": "12/01/2012"
        },
        {
            "number": "2015-030",
            "link": "http://www.google.com",
            "title": "my sample title",
            "awardClaimDueDate": "March 01, 2013",
            "awardClaimForms": "form-22",
            "datePosted": "12/01/2012"
        },
    ]
}

1 个答案:

答案 0 :(得分:2)

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class JsonMerge {

    public static void main(String[] args) throws IOException, JSONException {
// Read the json from the files
        FileInputStream fileOne = new FileInputStream(new File("D:\\jsonOne.json"));
        FileInputStream fileTwo = new FileInputStream(new File("D:\\jsonTwo.json"));
        String stringOne = null, stringTwo = null;
        try {
            stringOne = IOUtils.toString(fileOne);
            stringTwo = IOUtils.toString(fileTwo);
        } finally {
            fileOne.close();
            fileTwo.close();
        }

// Convert them into json objects
        JSONObject jsonOne = new JSONObject(stringOne);
        JSONObject jsonTwo = new JSONObject(stringTwo);

// Extract the arrays from the Notices Array
        JSONArray jsonOneNoticesArray = jsonOne.getJSONArray("Notices");
        JSONArray jsonTwoNoticesArray = jsonTwo.getJSONArray("Notices");
        JSONObject mergedJsonArray = new JSONObject();

// Iterate and compare the required condition
        for (int i = 0; i < jsonOneNoticesArray.length(); i++) {
            for (int j = 0; j < jsonTwoNoticesArray.length(); j++) {
                JSONObject mergedJson = new JSONObject();
                if (jsonOneNoticesArray.getJSONObject(i).getString("number").equals(jsonTwoNoticesArray.getJSONObject(j).getString("number"))) {
                    mergedJson.accumulate("number", jsonOneNoticesArray.getJSONObject(i).getString("number"));
                    mergedJson.accumulate("link", jsonOneNoticesArray.getJSONObject(i).getString("link"));
                    mergedJson.accumulate("title", jsonOneNoticesArray.getJSONObject(i).getString("title"));
                    mergedJson.accumulate("awardClaimDueDate", jsonOneNoticesArray.getJSONObject(i).getString("awardClaimDueDate"));
                    mergedJson.accumulate("datePosted", jsonOneNoticesArray.getJSONObject(i).getString("datePosted"));
                    mergedJson.accumulate("awardClaimForms", jsonTwoNoticesArray.getJSONObject(j).getString("awardClaimForms"));
                    mergedJsonArray.append("Notices", mergedJson);
                    break;
                }
            }

        }
        System.out.println("Done merging.. " + mergedJsonArray);
    }

}

添加以下行(而不是System.out.println行)可以将输出写入文件

FileUtils.write(new File("D:\\mergedJson.json"), mergedJsonArray.toString());

以下是上述代码中mergedJsonArray的输出。

enter image description here