我如何分析一个JSON数组在Android上使用杰克逊不同的对象? [英] How do I parse a JSON array into different objects using Jackson on Android?
问题描述
我试图解析JSON像Android上使用杰克逊以下为对象(注:我不是JSON格式的控制权 - 格式来自Yammer的)
引用:
{
类型:用户,
ID:12345678,
名:WEX
},
{
类型:消息,
ID:12345679,
身体:
{
普通:短消息
}
},
{
类型:线程,
ID:12345670,
thread_starter_id:428181699
}
]
问题是,在引用的每个条目
是一个不同类型具有不同属性的对象。作为一个开始我有:
公共静态类参考
{
公共字符串类型;
公共字符串ID;
}
我宁愿避免把所有潜在的性能在一个对象,如:
公共静态类参考
{
公共静态类体
{
公共字符串平原;
}
公共字符串类型;
公共字符串ID;
公共字符串名称;
公共机构机构;
公共字符串thread_starter_id;
}
和希望使用创建依赖于单独的类的键入
值,如:
公共静态类ReferenceUser扩展参考
{
公共字符串名称;
}公共静态类ReferenceMessage扩展参考
{
公共静态类体
{
公共字符串平原;
}
公共机构机构;
}公共静态类ReferenceThread扩展参考
{
公共字符串thread_starter_id;
}
所以...什么是让杰克逊解析这样的JSON的最佳方式?
我目前正在解析它很简单是这样的:
ObjectMapper映射器=新ObjectMapper();
参考文献[] =参考mapper.readValue(JSON,参考文献[]类);
你可以做这样的事情与杰克逊:
@JsonTypeInfo(
使用= JsonTypeInfo.Id.NAME,
包括= JsonTypeInfo.As.PROPERTY,
财产=类型)
@JsonSubTypes({
@ JsonSubTypes.Type(名称=用户,值= ReferenceUser.class)
@ JsonSubTypes.Type(名称=消息,值= ReferenceMessage.class)
@ JsonSubTypes.Type(NAME =线程,值= ReferenceThread.class)
})公共类参考{
INT ID;
字符串名称;
}
这样,您将生成的子类。
约翰
I'm trying to parse JSON like the following into object using Jackson on Android (Note: I'm not in control of the JSON format - the format comes from Yammer)
"references": [
{
"type": "user",
"id": 12345678,
"name": "Wex"
},
{
"type": "message",
"id": 12345679,
"body":
{
"plain":"A short message"
}
},
{
"type": "thread",
"id": 12345670,
"thread_starter_id": 428181699
}
]
The problem is that each entry in references
is a different type of object with different properties. As a start I've got:
public static class Reference
{
public String type;
public String id;
}
I'd rather avoid putting all potential properties in one object like:
public static class Reference
{
public static class Body
{
public String plain;
}
public String type;
public String id;
public String name;
public Body body;
public String thread_starter_id;
}
And want to use separate classes that are created dependant on the type
value like:
public static class ReferenceUser extends Reference
{
public String name;
}
public static class ReferenceMessage extends Reference
{
public static class Body
{
public String plain;
}
public Body body;
}
public static class ReferenceThread extends Reference
{
public String thread_starter_id;
}
So... what's the best way of getting Jackson to parse the JSON like that?
I'm currently parsing it quite simply like this:
ObjectMapper mapper = new ObjectMapper();
Reference[] references = mapper.readValue(json, Reference[].class);
you can do something like this with Jackson:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = "user", value = ReferenceUser.class),
@JsonSubTypes.Type(name = "message", value = ReferenceMessage.class),
@JsonSubTypes.Type(name = "thread", value = ReferenceThread.class)
})
public class Reference {
int id;
String name;
}
This way you will generate subclasses.
John
这篇关于我如何分析一个JSON数组在Android上使用杰克逊不同的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!