如何在jackson mapper的帮助下使用单个JSON文件创建多种类型的java对象?
我想使用一个JSON文件创建多种类型的java对象 我曾想过以map的形式存储不同类型的java对象,其中key是对象类型的名称,value是对象本身,如下所示 Field2_AllIn1.json如何在jackson mapper的帮助下使用单个JSON文件创建多种类型的java对象?,java,object,hashmap,jackson,geojson,Java,Object,Hashmap,Jackson,Geojson,我想使用一个JSON文件创建多种类型的java对象 我曾想过以map的形式存储不同类型的java对象,其中key是对象类型的名称,value是对象本身,如下所示 Field2_AllIn1.json { "fetchMapOfStringType": { "hi": "hello" }, "fetchMapFieldType": { "5b0b8a5545424a20487ee2bc": { "fieldId":
{
"fetchMapOfStringType": {
"hi": "hello"
},
"fetchMapFieldType": {
"5b0b8a5545424a20487ee2bc": {
"fieldId": "5b0b8a5545424a20487ee2bc",
"value": "25",
"values": [],
"visible": true,
"fieldValidationErrors": [],
"globalValidationErrors": [
{
"fieldValidationId": "5b0b8a5545424a20487ee2bc",
"validationErrorMessage": "this is error"
}
],
"formulaErrors": []
}
}
}
第一个存储一个简单的字符串类型
第二个存储map类型的java对象,其中Field是一个自定义类
public class JsonJacksonAllInOne {
public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException{
try {
// read JSON from a file
Map<String, Object> map = mapper.readValue(
new File("C://json/Field2_AllIn1.json"),
new TypeReference<Map<String, Object>>() {
});
//type String
//String type works fine
Object obj1 = map.get("fetchMapOfStringType");
Map<String, String> mapStringType = (Map<String, String>) obj1;
System.out.println(mapStringType.get("hi"));//this prints hello
//second type ... type casting to object type to objectId and Field map
Map<ObjectId, Field> map1 = (Map<ObjectId, Field>) map.get("fetchMapFieldType");
ObjectId k1 = new ObjectId("5b0b8a5545424a20487ee2bc");
Field f1 = map1.get(k1);
//this code gives value of f1 as null
// this gives class cast exception
// java.util.LinkedHashMap cannot be cast to com.dto.Field
Field f2 = map1.get("5b0b8a5545424a20487ee2bc");
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
公共类JSONJACKSONLINONE{
公共静态void main(字符串args[])抛出JsonParseException、JsonMappingException、IOException{
试一试{
//从文件中读取JSON
Map=mapper.readValue(
新文件(“C://json/Field2_AllIn1.json”),
新类型引用(){
});
//类型字符串
//字符串类型很好用
对象obj1=map.get(“fetchMapOfStringType”);
Map mapStringType=(Map)obj1;
System.out.println(mapStringType.get(“hi”);//这会打印hello
//第二种类型…类型转换为对象类型到objectId和字段映射
Map map1=(Map)Map.get(“fetchMapFieldType”);
ObjectId k1=新ObjectId(“5B0B8A55424A20487EE2BC”);
字段f1=map1.get(k1);
//此代码将f1的值设为null
//这将导致类强制转换异常
//无法将java.util.LinkedHashMap强制转换为com.dto.Field
字段f2=map1.get(“5b0b8a5545424a20487ee2bc”);
}捕获(JsonGenerationException e){
e、 printStackTrace();
}捕获(JsonMappingException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
正如您在代码中看到的注释。
一给零
其他则给出类强制转换异常
我猜演员阵容不好
如何获取所需类型的对象
在我可以使用单个JSON文件创建不同的java对象(如映射和自定义类列表)的情况下,还有其他方法吗?
我希望避免使用不同的JSON文件来创建不同的java对象问题在于这一行:
// read JSON from a file
Map<String, Object> map = mapper.readValue(
new File("C://json/Field2_AllIn1.json"),
new TypeReference<Map<String, Object>>() {
});
这节省了几个小时。非常感谢。
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
// read JSON from a file as a tree
JsonNode map = mapper.readTree(new File("/home/pratapi.patel/config.json"));
// fetch json node from the tree
JsonNode obj1 = map.get("fetchMapOfStringType");
// convert obj1 (JsonNode) as Map<String, String>
Map<String, String> mapStringType =
mapper.readValue(mapper.treeAsTokens(obj1), new TypeReference<Map<String, String>>() {
});
// prints hello
System.out.println(mapStringType.get("hi"));
// fetch another json node from the tree
JsonNode map1 = map.get("fetchMapFieldType");
// convert map1 (JsonNode) as Map<ObjectId, Field>
Map<ObjectId, Field> mapFieldType = mapper.readValue(mapper.treeAsTokens(map1), new TypeReference<Map<ObjectId, Field>>() {
});
ObjectId k1 = new ObjectId("5b0b8a5545424a20487ee2bc");
Field f1 = mapFieldType.get(k1);
System.out.println(f1);
} catch (IOException e) {
e.printStackTrace();
}
}
public class ObjectId {
private final String value;
@JsonCreator
public ObjectId(@JsonProperty("value") String value) {
super();
this.value = value;
}
public String getValue() {
return value;
}
/**
* Auto generated in eclipse
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
/**
* Auto generated in eclipse
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ObjectId other = (ObjectId) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}