Java 使用“反序列化JSON映射”;“关键”&&引用;“价值”;属性不适用于Jackson
问题Java 使用“反序列化JSON映射”;“关键”&&引用;“价值”;属性不适用于Jackson,java,android,json,serialization,jackson,Java,Android,Json,Serialization,Jackson,问题 @JsonIgnoreProperties(ignoreUnknown = true) public class Attributes { public Attributes() {} private ArrayList<Entry> entry; public ArrayList<Entry> getEntry() { return entry; } } 首先,下面的序列化JSON序列化有意义
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attributes {
public Attributes() {}
private ArrayList<Entry> entry;
public ArrayList<Entry> getEntry() {
return entry;
}
}
{
"attributes": {
"entry": [
{
"key": "operating system",
"value": "GNU/Linux"
},
{
"key": "allergies",
"value": "weed"
}
]
}
}
class Contact implements Comparable<Contact>, Serializable {
@JsonProperty("attributes")
private Map<String, String> attributes;
...
}
@JsonProperty("attributes")
private Map<String, List<Map<String, String>>> attributes;
POJO用于反序列化:
{
"attributes": {
"entry": [
{
"key": "operating system",
"value": "GNU/Linux"
},
{
"key": "allergies",
"value": "weed"
}
]
}
}
class Contact implements Comparable<Contact>, Serializable {
@JsonProperty("attributes")
private Map<String, String> attributes;
...
}
@JsonProperty("attributes")
private Map<String, List<Map<String, String>>> attributes;
反序列化后在调试器中检查属性对象:
{
"attributes": {
"entry": [
{
"key": "operating system",
"value": "GNU/Linux"
},
{
"key": "allergies",
"value": "weed"
}
]
}
}
class Contact implements Comparable<Contact>, Serializable {
@JsonProperty("attributes")
private Map<String, String> attributes;
...
}
@JsonProperty("attributes")
private Map<String, List<Map<String, String>>> attributes;
更改为后的进一步检查:
{
"attributes": {
"entry": [
{
"key": "operating system",
"value": "GNU/Linux"
},
{
"key": "allergies",
"value": "weed"
}
]
}
}
class Contact implements Comparable<Contact>, Serializable {
@JsonProperty("attributes")
private Map<String, String> attributes;
...
}
@JsonProperty("attributes")
private Map<String, List<Map<String, String>>> attributes;
@JsonProperty(“属性”)
私有地图属性;
依赖关系:
{
"attributes": {
"entry": [
{
"key": "operating system",
"value": "GNU/Linux"
},
{
"key": "allergies",
"value": "weed"
}
]
}
}
class Contact implements Comparable<Contact>, Serializable {
@JsonProperty("attributes")
private Map<String, String> attributes;
...
}
@JsonProperty("attributes")
private Map<String, List<Map<String, String>>> attributes;
- com.fasterxml.jackson.core:jackson-core:2.3.0
- core:jackson-databind:2.3.0
- core:jackson注释:2.3.0
Map
转换为json:
Map<String, String> map = new HashMap<String, String>();
map.put("operating system", "GNU/Linux");
map.put("allergies", "weed");
正如我们所看到的,没有键/值
解决方案
WrapperObject
@JsonIgnoreProperties(ignoreUnknown = true)
public class WrapperObject { // we can give any name to class, its only external {}
private Attributes attributes;
public WrapperObject() {}
public Attributes getAttributes() {
return attributes;
}
}
属性
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attributes {
public Attributes() {}
private ArrayList<Entry> entry;
public ArrayList<Entry> getEntry() {
return entry;
}
}
启动器
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String str = "{" +
" \"attributes\": {" +
" \"entry\": [" +
" {" +
" \"key\": \"operating system\"," +
" \"value\": \"GNU/Linux\"" +
" }," +
" {" +
" \"key\": \"allergies\"," +
" \"value\": \"weed\"" +
" }" +
" ]" +
" }" +
"}";
ObjectMapper mapper = new ObjectMapper();
WrapperObject mj = mapper.readValue(str, WrapperObject.class);
if(mj == null){
System.err.println("null");
}
// dummy check
System.out.println(mj.getAttributes().getEntry().get(0).getKey());
}
输出:
operating system