gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:52:15 25 4
gpt4 key购买 nike

我有多个如下所示的 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"
}

]
}

预期运营成本:

{
"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,我想将两个文件的映射结果合并到一个 o/p 文件中,如上面预期 o/p 中所述。

我正在尝试使用下面的代码来使用java,但它对于2个输入文件来说读得很好,但不确定如何di用于多个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);

}
}

最佳答案

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

//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;

和代码。

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);

obj1 和 obj2 会将列表映射结果作为列表。您可以将这些列表合并为一个列表,然后可以写入文件或在控制台上打印

编辑:

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

obj1.getMappingResult().putAll(obj2.getMappingResult());
String jsonString = objMapper.writeValueAsString(obj1);

关于java - 如何使用java读取多个JSON文件并将其写入单个JSON文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52074769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com