如何将 json 转换为没有给出参数名称的 pojo
how to convert json into pojo where no param name is given
我正在尝试使用 Jackson 将 JSON 转换为 java。但没有得到适当的解决方案。我有 JSON 其中没有参数名称。我想使用 PropertyOrder 将 json 字段映射到 POJO。
我尝试了所有可能的类型引用,但未能获得我想要的结果。
我的 JSON 是这样的:
{"1222": ["Joe", 26, 158],"1232": ["root", 29, 168] }
下面是 pojo:
public class Employee{
int empId;
EmployeeAtttribute employeeAttribute;
}
@JsonProertyOrder({"name", "seq", "height"})
public class EmployeeAttribute{
String name;
int seq;
int height;
}
我正在寻找使用 JSON 制作的员工列表 class。
提前致谢。
您的 json 将被解析为 Map<String, List<Object>>
。
之后,您可以将 Map<String, List<Object>>
转换为您的员工或更改 json 格式。 {"id":1222, "attribute":{"name":"Joe", "seq":26, "height": 158}}
将 EmployeeAttribute class 注释为:
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({"name", "seq", "height"})
public class EmployeeAttribute
{
public String name;
public int seq;
public int height;
@Override
public String toString()
{
return "EmployeeAttribute [name=" + name + ", seq=" + seq + ", height=" + height + "]";
}
}
您可以使用此代码将您的 JSON 转换为对象(地图):
ObjectMapper mapper = new ObjectMapper();
String jsonInput = "{\"1222\": [\"Joe\", 26, 158],\"1232\": [\"root\", 29, 168] }";
TypeReference<Map<String, EmployeeAttribute>> typeRef =
new TypeReference<Map<String, EmployeeAttribute>>()
{
};
Map<String, EmployeeAttribute> map = mapper.readValue(jsonInput, typeRef);
map.values().iterator().forEachRemaining(System.out::println);
进一步将其转换为 Employee 列表:
List<Employee> employee = new ArrayList<>();
for (Map.Entry<String, EmployeeAttribute> entry : map.entrySet()) {
employee.add(new Employee(Integer.valueOf(entry.getKey()),
entry.getValue()));
}
对于输入 JSON 字符串包含 'emp_count' 键的扩展要求,因为输入不能真正解析为 Java 对象模型,可以使用这种方法读取这个元素,然后删除它,这样按照原来的逻辑解析就会像以前一样工作,'emp_count' 仍然是 read/extracted。根据需要优化:
String jsonInput = "{\"1222\": [\"Joe\", 26, 158],\"1232\": [\"root\", 29, 168], \"emp_count\" : \"2\"}";
JsonNode node = mapper.readTree(jsonInput);
if (node.has("emp_count")) {
int employeesInArray = ((ObjectNode) node).remove("emp_count").asInt();
System.out.println("Num of employees in array: " + employeesInArray);
} else {
System.out.println("Num of employees was not provided, missing emp_count element");
}
//updated JSON input String, that works as before
jsonInput = node.toString();
我正在尝试使用 Jackson 将 JSON 转换为 java。但没有得到适当的解决方案。我有 JSON 其中没有参数名称。我想使用 PropertyOrder 将 json 字段映射到 POJO。
我尝试了所有可能的类型引用,但未能获得我想要的结果。
我的 JSON 是这样的: {"1222": ["Joe", 26, 158],"1232": ["root", 29, 168] }
下面是 pojo:
public class Employee{
int empId;
EmployeeAtttribute employeeAttribute;
}
@JsonProertyOrder({"name", "seq", "height"})
public class EmployeeAttribute{
String name;
int seq;
int height;
}
我正在寻找使用 JSON 制作的员工列表 class。
提前致谢。
您的 json 将被解析为 Map<String, List<Object>>
。
之后,您可以将 Map<String, List<Object>>
转换为您的员工或更改 json 格式。 {"id":1222, "attribute":{"name":"Joe", "seq":26, "height": 158}}
将 EmployeeAttribute class 注释为:
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({"name", "seq", "height"})
public class EmployeeAttribute
{
public String name;
public int seq;
public int height;
@Override
public String toString()
{
return "EmployeeAttribute [name=" + name + ", seq=" + seq + ", height=" + height + "]";
}
}
您可以使用此代码将您的 JSON 转换为对象(地图):
ObjectMapper mapper = new ObjectMapper();
String jsonInput = "{\"1222\": [\"Joe\", 26, 158],\"1232\": [\"root\", 29, 168] }";
TypeReference<Map<String, EmployeeAttribute>> typeRef =
new TypeReference<Map<String, EmployeeAttribute>>()
{
};
Map<String, EmployeeAttribute> map = mapper.readValue(jsonInput, typeRef);
map.values().iterator().forEachRemaining(System.out::println);
进一步将其转换为 Employee 列表:
List<Employee> employee = new ArrayList<>();
for (Map.Entry<String, EmployeeAttribute> entry : map.entrySet()) {
employee.add(new Employee(Integer.valueOf(entry.getKey()),
entry.getValue()));
}
对于输入 JSON 字符串包含 'emp_count' 键的扩展要求,因为输入不能真正解析为 Java 对象模型,可以使用这种方法读取这个元素,然后删除它,这样按照原来的逻辑解析就会像以前一样工作,'emp_count' 仍然是 read/extracted。根据需要优化:
String jsonInput = "{\"1222\": [\"Joe\", 26, 158],\"1232\": [\"root\", 29, 168], \"emp_count\" : \"2\"}";
JsonNode node = mapper.readTree(jsonInput);
if (node.has("emp_count")) {
int employeesInArray = ((ObjectNode) node).remove("emp_count").asInt();
System.out.println("Num of employees in array: " + employeesInArray);
} else {
System.out.println("Num of employees was not provided, missing emp_count element");
}
//updated JSON input String, that works as before
jsonInput = node.toString();