我需要使用Gson将传入的json消息解析为Java对象。 应该使用“ MessageBody”类来填充 Gson.fromJson(json,MessageBody.class);
json消息如下。
第一层有三个静态字段。第三个字段(“字段”)是“ DataField”对象的列表。
DataField对象具有类型和值字段。
它的值可以是异类的。预期的类型为:“字符串”,“整数”,“布尔值”和“ HashMap
{
"eventId": "abc",
"customerId": "abc",
"fields": {
"eventDateTime": {
"type": "datetime",
"value": "2019-05-03T10:15:30Z"
},
"eventCorrelationID": {
"type": "string",
"value": "abc"
},
"additionalAttributes": {
"type": "collection",
"value": {
"additionalAttribute1": {
"value": "abc",
"type": "string"
},
"additionalAttribute2": {
"value": "abc",
"type": "string"
}
}
}
}
}
public class MessageBody {
private String eventId;
private String customerId;
private HashMap<String, DataField> fields;
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public HashMap<String, DataField> getFields() {
return fields;
}
public void setFields(HashMap<String, DataField> fields) {
this.fields = fields;
}
public class DataField {
private Object value;
private String type;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
}
使用上述类进行的解析适用于除“ additionalAttributes”下的嵌套DataField列表以外的所有对象。
它们的结果为LinkedHashTreeMap对象。不幸的是,不可能将其转换为另一个DataField的HashMap。
如何处理异构对象中的嵌套/递归列表?
如何使以下语句起作用? :-)
HashMap<String, DataField> addAttrs = (HashMap<String, DataField>) messageBody.getFields().get("additionalAttributes").getValue();
答案 0 :(得分:0)
使用该结构,希望对您有所帮助:
public class MessageBody {
private String eventId;
private String customerId;
private DataFields datafields;
}
public class DataFields {
private DataField eventDateTime;
private DataField eventCorrelationID;
pprivate HashMap<String, DataField> additionalAttributes;
}
public class DataField {
private Object value;
private String type;
}
答案 1 :(得分:0)
您需要实现自定义JsonDeserializer
。您有类型信息,可能有助于区分正确的类型。下面的实现不使用它,但是如果您具有多个集合类型,则可以扩展它:
class DataFieldJsonDeserializer implements JsonDeserializer<DataField> {
private final Type collectionType = new TypeToken<Map<String, DataField>>() {}.getType();
@Override
public DataField deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
DataField dataField = new DataField();
JsonObject jsonObject = json.getAsJsonObject();
JsonPrimitive type = jsonObject.getAsJsonPrimitive("type");
dataField.setType(type.getAsString());
JsonElement value = jsonObject.get("value");
if (value.isJsonPrimitive()) {
dataField.setValue(value.getAsJsonPrimitive().getAsString());
} else {
Object result = context.deserialize(value, collectionType);
dataField.setValue(result);
}
return dataField;
}
}
注册适配器:
@JsonAdapter(DataFieldJsonDeserializer.class)
public static class DataField {
用法示例:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Gson gson = new GsonBuilder().create();
MessageBody messageBody = gson.fromJson(json, MessageBody.class);
Map<String, DataField> addAttrs = (Map<String, DataField>) messageBody.getFields()
.get("additionalAttributes").getValue();
System.out.println(addAttrs);
}
}
打印:
{additionalAttribute1=DataField{value=abc, type='string'}, additionalAttribute2=DataField{value=abc, type='string'}}