我的json回复
[
[
{
"header_id": 1,
"name" : "Tom",
"group" : "A"
},
{
"header_id": 2,
"name" : "Marry",
"group" : "B"
}
],
[
{
"object_id" : 1,
"SerialNo" : 123456
},
{
"object_id" : 2,
"SerialNo" : 89545
}
],
[
{
"workflow_id" : 1,
"StatusName" : "start"
},
{
"workflow_id" : 2,
"StatusName" : "end"
}
]
]
Header.class
public class Header{
@SerializedName("header_id")
private int HeaderID;
@SerializedName("name")
private String name;
@SerializedName("group")
private String group;
}
ObjectWork.class
public class ObjectWork {
@SerializedName("object_id")
private int ObjectID;
@SerializedName("SerialNo")
private int SerialNo;
}
WorkFlow.class
public class WorkFlow{
@SerializedName("workflow_id")
private int WorkflowID;
@SerializedName("StatusName")
private String statusName;
}
在json数组中,有三种类型的json对象。我将如何在retrofit2中设计pojo和自定义JsonDeserializer。现在我用gson解析这个。我不知道设计类来支持这个json并将它解串到pojo模型。
三江源
答案 0 :(得分:0)
按照链接直接从JSON创建一个pojo:http://www.jsonschema2pojo.org/
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("header_id")
@Expose
private Integer headerId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("group")
@Expose
private String group;
public Integer getHeaderId() {
return headerId;
}
public void setHeaderId(Integer headerId) {
this.headerId = headerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
答案 1 :(得分:0)
引入一个常见的包装器,让Gson识别出你的Gson禁止java.lang.Object
覆盖(注意推荐的fieldNamesCaseThatIsAlwaysCamelCase):
abstract class AbstractCommon {
}
final class Header
extends AbstractCommon {
@SerializedName("header_id")
final int headerId = Integer.valueOf(0);
@SerializedName("name")
final String name = null;
@SerializedName("group")
final String group = null;
@Override
public String toString() {
return headerId + ": " + name;
}
}
final class ObjectWork
extends AbstractCommon {
@SerializedName("object_id")
final int objectId = Integer.valueOf(0);
@SerializedName("SerialNo")
final int serialNo = Integer.valueOf(0);
@Override
public String toString() {
return objectId + ": " + serialNo;
}
}
final class WorkFlow
extends AbstractCommon {
@SerializedName("workflow_id")
final int workflowId = Integer.valueOf(0);
@SerializedName("StatusName")
final String statusName = null;
@Override
public String toString() {
return workflowId + ": " + statusName;
}
}
现在介绍一个尝试识别实际类型的JsonDeserializer
:
private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(AbstractCommon.class, (JsonDeserializer<AbstractCommon>) (jsonElement, type, context) -> {
final JsonObject jsonObject = jsonElement.getAsJsonObject();
if ( jsonObject.has("header_id") ) {
return context.deserialize(jsonElement, Header.class);
}
if ( jsonObject.has("object_id") ) {
return context.deserialize(jsonElement, ObjectWork.class);
}
if ( jsonObject.has("workflow_id") ) {
return context.deserialize(jsonElement, WorkFlow.class);
}
throw new IllegalArgumentException("Cannot recognize: " + jsonElement);
})
.create();
现在你可以轻松地做到:
private static final Type commonsType = new TypeToken<List<List<AbstractCommon>>>() {
}.getType();
public static void main(final String... args)
throws IOException {
try ( final JsonReader jsonReader = getPackageResourceJsonReader(Q44034331.class, "arrays.json") ) {
final List<List<AbstractCommon>> commons = gson.fromJson(jsonReader, commonsType);
commons.stream()
.flatMap(Collection::stream)
.forEach(abstractCommon -> System.out.println(abstractCommon.getClass().getSimpleName() + ": " + abstractCommon));
}
}
输出:
标题:1:汤姆 标题:2:嫁给 ObjectWork:1:123456
ObjectWork:2:89545
WorkFlow:1:开始
WorkFlow:2:结束
坦率地说,我觉得你的JSON有点奇怪(假设子阵列元素真的看起来彼此相关但是解构了):它有点像一个由内到外的结构。如果你可以控制JSON格式,我真的很赞赏你重新设计它。如果它不在您的控制之下,看起来您需要这样的东西(Gson类型适配器也可以):
final class Wrapper {
final Header header;
final ObjectWork objectWork;
final WorkFlow workFlow;
Wrapper(final Header header, final ObjectWork objectWork, final WorkFlow workFlow) {
this.header = header;
this.objectWork = objectWork;
this.workFlow = workFlow;
}
}
private static Collection<Wrapper> combine(final List<List<AbstractCommon>> commons) {
@SuppressWarnings({ "unchecked", "rawtypes" })
final List<Header> headers = (List) commons.get(0);
@SuppressWarnings({ "unchecked", "rawtypes" })
final List<ObjectWork> objectWorks = (List) commons.get(1);
@SuppressWarnings({ "unchecked", "rawtypes" })
final List<WorkFlow> workFlows = (List) commons.get(2);
final Iterator<Header> headerIterator = headers.iterator();
final Iterator<ObjectWork> objectWorkIterator = objectWorks.iterator();
final Iterator<WorkFlow> workFlowIterator = workFlows.iterator();
final Collection<Wrapper> wrappers = new ArrayList<>();
while ( headerIterator.hasNext() && objectWorkIterator.hasNext() && workFlowIterator.hasNext() ) {
final Header header = headerIterator.next();
final ObjectWork objectWork = objectWorkIterator.next();
final WorkFlow workFlow = workFlowIterator.next();
final Wrapper wrapper = new Wrapper(header, objectWork, workFlow);
wrappers.add(wrapper);
}
return wrappers;
}
combine(commons)
.forEach(wrapper -> System.out.println(wrapper.header + ", " + wrapper.objectWork + ", " + wrapper.workFlow));
输出:
1:汤姆,1:123456,1:开始
2:结婚,2:89545,2:结束