2 回答

TA貢獻1873條經(jīng)驗 獲得超9個贊
您可以從Jackson
庫中使用JsonTypeInfo
和JsonSubTypes
注釋。它們是句柄多態(tài)類型處理:
@JsonTypeInfo
用于指示序列化中包含哪些類型信息的詳細信息@JsonSubTypes
用于表示注解類型的子類型@JsonTypeName
用于定義用于注釋類的邏輯類型名稱
您的示例適合此解決方案,除了看起來更像簡單POJO
類的根對象。在您的情況下,我們應(yīng)該創(chuàng)建有助于使用以下 3 種類型的類型結(jié)構(gòu):string
, date
, map
:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = StringValue.class, name = "string"),
@JsonSubTypes.Type(value = DateValue.class, name = "date"),
@JsonSubTypes.Type(value = MapValue.class, name = "map")
})
abstract class HasValue<T> {
protected T value;
public HasValue() {
this(null);
}
public HasValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"value=" + value +
"}";
}
}
class StringValue extends HasValue<String> {
public StringValue() {
this(null);
}
public StringValue(String value) {
super(value);
}
}
class DateValue extends HasValue<String> {
public DateValue(String value) {
super(value);
}
public DateValue() {
this(null);
}
}
class MapValue extends HasValue<Map<String, HasValue>> {
public MapValue(Map<String, HasValue> value) {
super(value);
}
public MapValue() {
this(new LinkedHashMap<>());
}
public void put(String key, HasValue hasValue) {
this.value.put(key, hasValue);
}
}
現(xiàn)在,我們需要引入POJO根值。它可能如下所示,但您可以根據(jù)需要添加 getter/setter。對于這個例子,下面的代碼就足夠了:
class Root {
public HasValue firstObject;
public HasValue secondObject;
public HasValue thirdObject;
public HasValue fourthObject;
@Override
public String toString() {
return "Root{" +
"firstObject=" + firstObject +
", secondObject=" + secondObject +
", thirdObject=" + thirdObject +
", fourthObject=" + fourthObject +
'}';
}
}
現(xiàn)在,我們終于可以嘗試序列化和反序列化這些對象了:
MapValue customerName = new MapValue();
customerName.put("customerName", new StringValue("customerValue"));
MapValue innerMap = new MapValue();
innerMap.put("anotherObj1", new StringValue("TEST"));
innerMap.put("anotherObj2", new DateValue("01/12/2018"));
innerMap.put("anotherObj3", new DateValue("31/01/2018"));
MapValue fourthObject = new MapValue();
fourthObject.put("firstObject", innerMap);
Root root = new Root();
root.firstObject = new StringValue("productValue");
root.secondObject = new StringValue("statusValue");
root.thirdObject = customerName;
root.fourthObject = fourthObject;
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
System.out.println(json);
System.out.println(mapper.readValue(json, Root.class));
Aboce 代碼打印JSON:
{
"firstObject" : {
"type" : "string",
"value" : "productValue"
},
"secondObject" : {
"type" : "string",
"value" : "statusValue"
},
"thirdObject" : {
"type" : "map",
"value" : {
"customerName" : {
"type" : "string",
"value" : "customerValue"
}
}
},
"fourthObject" : {
"type" : "map",
"value" : {
"firstObject" : {
"type" : "map",
"value" : {
"anotherObj1" : {
"type" : "string",
"value" : "TEST"
},
"anotherObj2" : {
"type" : "date",
"value" : "01/12/2018"
},
"anotherObj3" : {
"type" : "date",
"value" : "31/01/2018"
}
}
}
}
}
}
和toString表示:
Root{firstObject=StringValue{value=productValue}, secondObject=StringValue{value=statusValue}, thirdObject=MapValue{value={customerName=StringValue{value=customerValue}}}, fourthObject=MapValue{value={firstObject=MapValue{value={anotherObj1=StringValue{value=TEST}, anotherObj2=DateValue{value=01/12/2018}, anotherObj3=DateValue{value=31/01/2018}}}}}}
您可以通過添加/刪除任何類型的HasValue實例輕松地操作輸出。

TA貢獻1834條經(jīng)驗 獲得超8個贊
從這里下載 JSON jar 。從客戶端使用 json.stringify 將您的 JSON 轉(zhuǎn)換為字符串(因為 JSONObject 的構(gòu)造函數(shù)只接受字符串)。收到客戶端的請求后,執(zhí)行以下操作:
public void doPost(request,response) throws ParseException, JSONException {
parseMapFromJSON(request.getParameter("JSONFromClient"));
}
private void parseMapFromJSON(String JSONParam) throws JSONException
{
JSONObject requestJSON = new JSONObject(JSONParam);
for(int i=0; i<requestJSON.length();i++)
{
String key = (String) requestJSON.names().get(i);
if(key.endsWith("Object"))
{
parseMapFromJSON(requestJSON.get(key).toString());
}
else if(key.startsWith("type") && (requestJSON.get(key).equals("date") || requestJSON.get(key).equals("string")))
{
System.out.println(requestJSON.get("value"));
break;
}
else if(key.startsWith("type") && requestJSON.get(key).equals("map"))
{
parseMapFromJSON(requestJSON.get("value").toString());
}
else if(!key.equals("value"))
{
parseMapFromJSON(requestJSON.get(key).toString());
}
}
}
添加回答
舉報