我有一个JSON结构:
{
"dataArray": [
{
"key": "prodData",
"format": "dataCollection",
"data": [
{
"key": "reference",
"format": "vector",
"type": "str",
"data": [
"SAM",
"PETER",
"SMITH"
]
},
{
"key": "rank",
"format": "vector",
"type": "str",
"data": [
"1st",
"2nd",
"3rd"
]
},
{
"key": "designation",
"format": "vector",
"type": "str",
"data": [
"MGR",
"DIR",
"JNR"
]
},
{
"key": "salary",
"format": "vector",
"type": "float",
"data": [
"2.1",
"3.1",
"1.1"
]
}
]
}
]
}
而且我想从上述结构中创建Java对象,例如:
Data ("SAM", "1st", "MGR", 2.1)
Data ("PETER", "2nd", "DIR", 3.1)
Data ("SMITH", "3rd", "JNR", 1.1)
[请让我知道Java中的哪个库会自动执行此转换。我不想对单个数组进行任何循环,请分享任何示例。谢谢
根据您的目标和结构,有很多不同的选择。 JSON-B和Jackson是较常见的。
根据您要尝试执行的操作,某些框架甚至以透明的方式执行此操作。例如Quarkus和Spring Boot具有相应的依赖性。
因此,您可以使用com.fasterxml.jackson.core将String
转换为类对象,然后使用for
循环获取所需的数据。下面是该代码:
您执行所有工作的主要方法:
public static void main(String[] args) throws Exception {
String input = "{\"dataArray\":[{\"key\":\"prodData\",\"format\":\"dataCollection\",\"data\":[{\"key\":\"reference\",\"format\":\"vector\",\"type\":\"str\",\"data\":[\"SAM\",\"PETER\",\"SMITH\"]},{\"key\":\"rank\",\"format\":\"vector\",\"type\":\"str\",\"data\":[\"1st\",\"2nd\",\"3rd\"]},{\"key\":\"designation\",\"format\":\"vector\",\"type\":\"str\",\"data\":[\"MGR\",\"DIR\",\"JNR\"]},{\"key\":\"salary\",\"format\":\"vector\",\"type\":\"float\",\"data\":[\"2.1\",\"3.1\",\"1.1\"]}]}]}";
ObjectMapper mapper = new ObjectMapper();
RootData rootData = mapper.readValue(input, RootData.class);
List<DataModel> dataModels = rootData.dataArray.get(0).data;
Collection<Destination> output = convertToDataModel(dataModels);
// puts out all you need
}
private static Collection<Destination> convertToDataModel(List<DataModel> dataModels) {
Map<Integer, Map<String, String>> target = new HashMap<>();
for (DataModel dataModel : dataModels) {
int counter = 0;
for (String data : dataModel.data) {
if (!target.containsKey(counter))
target.put(counter, new HashMap<>());
target.get(counter).put(dataModel.key, data);
counter++;
}
}
return target.values().stream()
.map(fields -> new Destination(fields.get("reference"), fields.get("rank"), fields.get("designation"), fields.get("salary")))
.collect(Collectors.toList());
}
如果要处理dataArray
的所有元素,而不仅是第一个元素(因为JSON
仅具有一个元素,则可以用以下命令替换输出行:
Collection<Destination> allOutputs = rootData.dataArray.stream()
.map(x-> convertToDataModel(x.data))
.flatMap(Collection::stream) // remove this line if you want a collection of collection instead of a single collection of the Destination
.collect(Collectors.toList());
您的JSON数据的类结构
public class RootData {
@JsonProperty("dataArray")
private List<SubRootData> dataArray;
}
public class SubRootData {
@JsonProperty("key")
private String key;
@JsonProperty("format")
private String format;
@JsonProperty("data")
private List<DataModel> data;
}
public class DataModel {
@JsonProperty("key")
private String key;
@JsonProperty("format")
private String format;
@JsonProperty("type")
private String type;
@JsonProperty("data")
private Collection<String> data;
}
您的输出类别:
public static class Destination {
private String reference;
private String rank;
private String designation;
private String salary;
public Destination(String reference, String rank, String designation, String salary) {
this.reference = reference;
this.rank = rank;
this.designation = designation;
this.salary = salary;
}
}