如何将Json转换为Java bean对象?

时间:2019-01-13 03:12:16

标签: java android

我正在尝试将复杂的对象转换为相关的Java Bean。但是,某些子类可能无法正确生成。

我只是使用模拟MS自适应卡来创建一组Java Bean类。当我调用Gson包或阿里巴巴fastJson包来解析我的json数据时。它始终显示超类类型。

这只是测试Gson和fastJson是否可以转换复杂对象的实验。在Android Studio上运行。

我的演示json如下:

{
    "type": "AdaptiveCard",
    "version": "1.0",
    "id": "workloadQCactivity 20",
    "speak": "activity 20 <break time=\"300ms\"/> at<break time=\"300ms\"/> <break time=\"300ms\"/>building<break time=\"300ms\"/>A<break time=\"300ms\"/>floor<break time=\"300ms\"/>1<break time=\"300ms\"/>room<break time=\"300ms\"/>1<break time=\"300ms\"/> ",
    "body": [{
        "type": "Container",
        "items": [{
            "type": "ColumnSet",
            "columns": [{
                "type": "Column",
                "width": "Stretch",
                "items": [{
                    "type": "TextBlock",
                    "size": "large",
                    "weight": "bolder",
                    "text": "activity 20 at building A floor 1 room 1",
                    "wrap": true
                }]
            }]
        }]
    }],
    "actions": [{
        "type": "Action.ShowCard",
        "card": {
            "type": "AdaptiveCard",
            "version": "1.0",
            "body": [{
                "type": "TextBlock",
                "size": "medium",
                "weight": "bolder",
                "isSubtle": true,
                "text": "have thing to check list1",
                "wrap": true
            }, {
                "type": "TextBlock",
                "weight": "bolder",
                "isSubtle": true,
                "text": "this is section 1",
                "wrap": true
            }, {
                "type": "TextBlock",
                "isSubtle": true,
                "text": "q1 of s1",
                "wrap": true
            }, {
                "type": "TextBlock",
                "isSubtle": true,
                "text": "q2 of s2",
                "wrap": true
            }, {
                "type": "TextBlock",
                "weight": "bolder",
                "isSubtle": true,
                "text": "this is section 2",
                "wrap": true
            }, {
                "type": "TextBlock",
                "isSubtle": true,
                "text": "q1 of s22",
                "wrap": true
            }, {
                "type": "TextBlock",
                "size": "medium",
                "weight": "bolder",
                "isSubtle": true,
                "text": "have checklist 2",
                "wrap": true
            }, {
                "type": "TextBlock",
                "weight": "bolder",
                "isSubtle": true,
                "text": "section of the second checklist",
                "wrap": true
            }, {
                "type": "TextBlock",
                "isSubtle": true,
                "text": "qqqqqq",
                "wrap": true
            }]
        },
        "title": "Show Checklist"
    }]
}

因此,我只是按照MS自适应卡来创建以下Java Bean。

第一类:AdaptiveTypedElement

public class AdaptiveTypedElement {

@JsonProperty("additionalPorperties")
public Map<String, Object> additionalPorperties = new HashMap<String, Object>();

//@JsonProperty("type")
//public String type;

@JsonProperty("id")
public String id;

}

第二类:AdaptiveTypedElement

public class AdaptiveElement extends  AdaptiveTypedElement {

@JsonProperty("spacing")
public AdaptiveSpacing spacing ;

@JsonProperty("separator")
public boolean separator = false;

@JsonProperty("speak")
public String speak;

@JsonProperty("separation")
// public AdaptiveSeparationStyle separation;
public String separation;

}

第三类AdaptiveContainer:

public class AdaptiveContainer extends AdaptiveElement {

@JsonProperty("typeName")
public String typeName = "Container";

@JsonProperty("type")
public String type = "Container";

@JsonProperty("items")
public List<AdaptiveElement> items = new ArrayList<AdaptiveElement>();

@JsonProperty("selectAction")
public AdaptiveAction selectAction = null;

@JsonProperty("style")
public AdaptiveContainerStyle style = AdaptiveContainerStyle.Default;

} 


public class AdaptiveColumnSet extends AdaptiveElement {

@JsonProperty("typeName")
public final String typeName  = "ColumnSet";

@JsonProperty("type")
public final String type  = "ColumnSet";

@JsonProperty("columns")
public List<AdaptiveColumn> columns = new ArrayList<AdaptiveColumn>();

@JsonProperty("selectionAction")
public AdaptiveAction selectionAction = null;
}


public class AdaptiveColumn extends  AdaptiveContainer{

@JsonProperty("typeName")
public final String typeName  = "Column";

@JsonProperty("type")
public final String type  = "Column";

@JsonProperty("size")
public String size;

@JsonProperty("with")
public String with;
}

public class AdaptiveAction {

@JsonProperty("title")
public  String title;

@JsonProperty("speak")
public  String speak;
}


public class AdaptiveShowCardAction extends  AdaptiveAction {

@JsonProperty("typeName")
public final String typeName = "Action.ShowCard";

@JsonProperty("type")
public final String Type  = "Action.ShowCard";

@JsonProperty("card")
public AdaptiveCard card;
}

public class AdaptiveTextBlock extends  AdaptiveElement{

@JsonProperty("typeName")
public String typeName = "TextBlock";

@JsonProperty("type")
public String type = "TextBlock";

@JsonProperty("text")
public String text = "";

@JsonProperty("size")
public AdaptiveTextSize size;

@JsonProperty("weight")
public AdaptiveTextWeight weight;

@JsonProperty("color")
public AdaptiveTextColor color;

@JsonProperty("horizontalAlignment")
public AdaptiveHorizontalAlignment horizontalAlignment = AdaptiveHorizontalAlignment.Left;

@JsonProperty("wrap")
public boolean wrap = false;

@JsonProperty("isSubtle")
public boolean isSubtle = false;

@JsonProperty("maxLines")
public int maxLines = 0;

@JsonProperty("maxWidth")
public int maxWidth = 0;

}

public class AdaptiveCard extends AdaptiveTypedElement {

@JsonProperty("contentType")
public final String contentType = "application/vnd.microsoft.card.adaptive";

@JsonProperty("typeName")
public final String typeName = "AdaptiveCard";

@JsonProperty("type")
public String type = "AdaptiveCard";

@JsonProperty("body")
public List<AdaptiveElement> body = new ArrayList<AdaptiveElement>();

@JsonProperty("actions")
public List<AdaptiveAction> actions = new ArrayList<AdaptiveAction>();

@JsonProperty("speak")
public String speak = null;

@JsonProperty("title")
public String title;

@JsonProperty("version")
//public AdaptiveSchemaVersion version = null;
public String version = null;

@JsonProperty("fallbackText")
public String fallbackText = null;

@JsonProperty("lang")
public String lang = null;
}

最后,我终于得到了AdaptiveCard对象。查看我的代码:

return JSON.parseObject(attachJson, AdaptiveCard.class)。请注意,我在Android Studio中添加了“实现'com.alibaba:fastjson:1.2.54'”

当我检查该对象时,我发现Body数据成员应该是“ AdaptiveContainer”类而不是“ AdaptiveElement”。我想知道为什么它没有遵循OOP和OOD的子类机制。我期望使用“ AdaptiveContainer”,但实际上输出是“ AdaptiveElement”。

enter image description here

2 个答案:

答案 0 :(得分:0)

rib-pet,您的问题有一个很大的例子。无论如何,我尝试使用GSON为您的示例创建Java类映射

Action.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Action {

@SerializedName("type")
@Expose
private String type;
@SerializedName("card")
@Expose
private Card card;
@SerializedName("title")
@Expose
private String title;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Card getCard() {
return card;
}

public void setCard(Card card) {
this.card = card;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

}

Body.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Body {

@SerializedName("type")
@Expose
private String type;
@SerializedName("items")
@Expose
private List<Item> items = null;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public List<Item> getItems() {
return items;
}

public void setItems(List<Item> items) {
this.items = items;
}

}

Body_.java(取决于您希望的模型,您应该在现有的某些类中更好地集成此类

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Body_ {

@SerializedName("type")
@Expose
private String type;
@SerializedName("size")
@Expose
private String size;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("isSubtle")
@Expose
private Boolean isSubtle;
@SerializedName("text")
@Expose
private String text;
@SerializedName("wrap")
@Expose
private Boolean wrap;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public Boolean getIsSubtle() {
return isSubtle;
}

public void setIsSubtle(Boolean isSubtle) {
this.isSubtle = isSubtle;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Boolean getWrap() {
return wrap;
}

public void setWrap(Boolean wrap) {
this.wrap = wrap;
}

}

Card.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Card {

@SerializedName("type")
@Expose
private String type;
@SerializedName("version")
@Expose
private String version;
@SerializedName("body")
@Expose
private List<Body_> body = null;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public List<Body_> getBody() {
return body;
}

public void setBody(List<Body_> body) {
this.body = body;
}

}

Column.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Column {

@SerializedName("type")
@Expose
private String type;
@SerializedName("width")
@Expose
private String width;
@SerializedName("items")
@Expose
private List<Item_> items = null;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getWidth() {
return width;
}

public void setWidth(String width) {
this.width = width;
}

public List<Item_> getItems() {
return items;
}

public void setItems(List<Item_> items) {
this.items = items;
}

}

Example.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("type")
@Expose
private String type;
@SerializedName("version")
@Expose
private String version;
@SerializedName("id")
@Expose
private String id;
@SerializedName("speak")
@Expose
private String speak;
@SerializedName("body")
@Expose
private List<Body> body = null;
@SerializedName("actions")
@Expose
private List<Action> actions = null;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getSpeak() {
return speak;
}

public void setSpeak(String speak) {
this.speak = speak;
}

public List<Body> getBody() {
return body;
}

public void setBody(List<Body> body) {
this.body = body;
}

public List<Action> getActions() {
return actions;
}

public void setActions(List<Action> actions) {
this.actions = actions;
}

}

Item.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Item {

@SerializedName("type")
@Expose
private String type;
@SerializedName("columns")
@Expose
private List<Column> columns = null;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public List<Column> getColumns() {
return columns;
}

public void setColumns(List<Column> columns) {
this.columns = columns;
}

}

Item_.java也需要更好的集成

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Item_ {

@SerializedName("type")
@Expose
private String type;
@SerializedName("size")
@Expose
private String size;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("text")
@Expose
private String text;
@SerializedName("wrap")
@Expose
private Boolean wrap;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Boolean getWrap() {
return wrap;
}

public void setWrap(Boolean wrap) {
this.wrap = wrap;
}

}

希望有帮助!

答案 1 :(得分:0)

我已经通过以下解决方案找到了解决方案:

JSONObject和JSONArray

JSONArray body = contentObj.getJSONArray("body");

JSONArray actions = contentObj.getJSONArray("actions");

title.setText(body.getJSONObject(0).getString("text"));