我想使用Java来接收JSON主体,遍历记录,并将每个其他记录输出到新的JSON数组中。每组两个记录都是它自己的数组,我只需要在每个相应的数组中取第一个。我还将在原始请求中提供需要添加到后续展平对象的列名称。例如,假设我有以下JSON请求体:
{
"records": [
[
[
"0DFC29E2-700E-4CC1-931E-B61DF4954B6B",
"John Doe",
"Teacher",
"China"
],
[
"B5B9186E-CE65-4911-8516-C510D3CC3ACE",
"Jane Doe",
"Doctor",
"London"
]
],
[
[
"20C4DD07-4E96-47F8-A1E1-B20B4C48120C",
"Jim Doe",
"Lawyer",
"Canada"
],
[
"76718CB1-238F-418E-BD14-5E2867FF3FB4",
"Jack Doe",
"Chef",
"Mexico"
]
]
],
"columns": [
"ID",
"Name",
"Occupation",
"Location"
]
}
然后我希望这个请求正文变为以下内容:
[{
"ID": "0DFC29E2-700E-4CC1-931E-B61DF4954B6B",
"Name": "John Doe",
"Occupation": "Teacher",
"Location": "China"
},
{
"ID": "20C4DD07-4E96-47F8-A1E1-B20B4C48120C",
"Name": "Jim Doe",
"Occupation": "Lawyer",
"Location": "Canada"
}]
我希望这段代码非常动态,因此它不会在代码中显式引用列名。这样我将来可以传递其他列名称,如果我有一个不同的JSON体结构,它将相应地工作。我将永远传递带有“记录”标题的数据,这样硬编码就可以了。非常感谢任何帮助。
答案 0 :(得分:0)
您可以将源JSON读入一堆Java对象,在Java端进行转换,然后以新格式输出。
如果有一个等效于XSLT的JSON会很好,但我还没有看到一个通用的。
答案 1 :(得分:0)
您应该将源JSON转换为地图集合。每个地图都将包含属性名称和属性值。之后,您可以轻松地将其序列化为预期格式。在下面的示例中,我使用了Jackson库,但我认为您也应该能够使用Gson库。
首先,我们应该定义SourceEntity
类,它定义输入JSON的所有属性。
class SourceEntity {
private String[][][] records;
private String[] columns;
public String[][][] getRecords() {
return records;
}
public void setRecords(String[][][] records) {
this.records = records;
}
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
}
}
之后,我们应该编写转换器,它可以解析输入JSON,将数组转换为映射集合并将其序列化为目标JSON。
class JsonConverter {
private ObjectMapper objectMapper = new ObjectMapper();
private JsonFactory jsonFactory = new JsonFactory();
public String convert(File sourceJsonFile) throws Exception {
SourceEntity sourceEntity = parseSourceEntity(sourceJsonFile);
List<Map<String, String>> result = convertToTargetPropertiesMap(sourceEntity);
return objectMapper.writeValueAsString(result);
}
private SourceEntity parseSourceEntity(File sourceJsonFile)
throws Exception {
JsonParser parser = jsonFactory.createJsonParser(sourceJsonFile);
return objectMapper.readValue(parser, SourceEntity.class);
}
private List<Map<String, String>> convertToTargetPropertiesMap(
SourceEntity entity) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (String[][] pairs : entity.getRecords()) {
list.add(createPropertyMap(entity.getColumns(), pairs[0]));
}
return list;
}
private Map<String, String> createPropertyMap(String[] names,
String[] values) {
Map<String, String> propertyMap = new LinkedHashMap<String, String>();
for (int i = 0; i < values.length; i++) {
propertyMap.put(names[i], values[i]);
}
return propertyMap;
}
}
最后,我们应该写一点测试:
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonProgram {
public static void main(String[] args) throws Exception {
JsonConverter converter = new JsonConverter();
String result = converter.convert(new File("/tmp/source.json"));
System.out.println(result);
}
}
以上程序打印此JSON作为示例输入:
[{"ID":"0DFC29E2-700E-4CC1-931E-B61DF4954B6B","Name":"John Doe","Occupation":"Teacher","Location":"China"},{"ID":"20C4DD07-4E96-47F8-A1E1-B20B4C48120C","Name":"Jim Doe","Occupation":"Lawyer","Location":"Canada"}]