如何使用Java读取多个JSON文件并将其写入单个JSON文件

时间:2018-08-29 10:05:04

标签: java json java-8

我有如下所示的多个JSON文件,并希望合并文件并将其中的数据存储到新的单个json文件中,如下所述:

JSON1:

{
    "jobRunID" : "1940",
    "mappingResult": [

        {
            "studyID": "ca209-030",
            "fileName": "aesae.csv",
            "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
            "columnName": "ae_start_dt",
            "currentMapping": "ae_start_dt,ae_st_dt",
            "isMapped": "N",
            "timeStamp": "20180827093033"
        },
        {
            "studyID": "ca209-004",
            "fileName": "labressae.csv",
            "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
            "columnName": "ae_verbatim",
            "currentMapping": "ae_vtm,ae_vt",
            "isMapped": "N",
            "timeStamp": "20180827093033"
        }

    ]
}

JSON2:

{
    "jobRunID" : "1940",
    "mappingResult": [

        {
            "studyID": "ca209-030",
            "fileName": "aesae.csv",
            "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
            "columnName": "ae_start_dt",
            "currentMapping": "ae_start_dt,ae_st_dt",
            "isMapped": "N",
            "timeStamp": "20180827093033"
        },
        {
            "studyID": "ca209-004",
            "fileName": "labressae.csv",
            "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
            "columnName": "ae_verbatim",
            "currentMapping": "ae_vtm,ae_vt",
            "isMapped": "N",
            "timeStamp": "20180827093033"
        }

    ]
}

预期的O / P:

{
  "jobRunID": "1940",
  "mappingResult": [
    {
      "studyID": "ca209-030",
      "fileName": "aesae.csv",
      "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
      "columnName": "ae_start_dt",
      "currentMapping": "ae_start_dt,ae_st_dt",
      "isMapped": "N",
      "timeStamp": "20180827093033"
    },
    {
      "studyID": "ca209-004",
      "fileName": "labressae.csv",
      "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
      "columnName": "ae_verbatim",
      "currentMapping": "ae_vtm,ae_vt",
      "isMapped": "N",
      "timeStamp": "20180827093033"
    },
    {
      "studyID": "ca209-030",
      "fileName": "aesae.csv",
      "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/aesae.csv",
      "columnName": "ae_start_dt",
      "currentMapping": "ae_start_dt,ae_st_dt",
      "isMapped": "N",
      "timeStamp": "20180827093033"
    },
    {
      "studyID": "ca209-004",
      "fileName": "labressae.csv",
      "ref2FilePath": "C://stage/pra/cro/ca/209/030/ref2/labressae.csv",
      "columnName": "ae_verbatim",
      "currentMapping": "ae_vtm,ae_vt",
      "isMapped": "N",
      "timeStamp": "20180827093033"
    }
  ]
}

任何帮助将不胜感激,因为我是JAVA中新处理JSON文件的人。 按照上述所有相同JobID的JOSN,我希望将两个文件中的mappingResult合并到一个o / p文件中,如上面预期的o / p中所述。

我正在尝试通过使用以下代码来使用Java,但是它对于2个输入文件的读取效果很好,但不确定如何对多个json文件进行编码:

public class MergeJSON {
    @JsonMerge
    List<Integer> contacts;

    public List<Integer> getContacts() {
        return contacts;
    }

    public void setContacts(List<Integer> contacts) {
        this.contacts = contacts;
    }

    @Override
    public String toString() {
        return contacts.toString();
    }
    public static void main(String[] args) throws JsonProcessingException, IOException {
        TypeReference<Map<String, MergeJSON>> type = new TypeReference<Map<String, MergeJSON>>() {};
        InputStream input = new ClassPathResource("C:\\Users\\sweta.h.sharma\\Test.json").getInputStream();
        InputStream input2 = new ClassPathResource("C:\\Users\\sweta.h.sharma\\Test1.json").getInputStream();
        ObjectMapper mapper = new ObjectMapper();
        Object contacts = mapper.readValue(input, type);
        mapper.reader(type)
                .withValueToUpdate(contacts)
                .readValues(input2);

            System.out.println(contacts);

}
}

1 个答案:

答案 0 :(得分:1)

使用Jackson Api。
创建与您的json对象相同的Pojo类(该类应具有“ jobRunID”,“ mappingResult”之类的成员)。

category_id

这是您必须使用的类

//class JsonObj with getters & setters 
String jobRunID;
List<Mappingresult> mappingResult;  // Mappingresult is again POJO class with members like studyID,fileName etc

和代码。

com.fasterxml.jackson.core.JsonFactory;
com.fasterxml.jackson.core.JsonParser;
com.fasterxml.jackson.databind.ObjectMapper;

obj1和obj2将具有列表mappingResult作为列表。您可以将这些列表合并为一个列表,然后可以在控制台中写入文件或打印

编辑:

要以字符串形式返回结果,可以使用ObjectMapper :: writeValueAsString

ObjectMapper objMapper=new ObjectMapper();
JsonFactory jfactory = new JsonFactory();
JsonParser jParser1=jfactory.createJsonParser(jsonString1); //json String 1
JsonParser jParser2=jfactory.createJsonParser(jsonString2); //json String 2

JsonObj obj1=objMapper.readValue(jParser1,JsonObj.class);// JsonObj is Pojo for your jsonObject
JsonObj obj2=objMapper.readValue(jParser2,JsonObj.class);