簡體   English   中英

如何將Json轉換為Java bean對象?

[英]How to convert Json into a Java bean object?

我正在嘗試將復雜的對象轉換為相關的Java Bean。 但是,某些子類可能無法正確生成。

我只是使用模擬MS Adaptive卡來創建一組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”。

在此處輸入圖片說明

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;
}

}

希望能幫助到你!

我已經通過以下解決方案找到了解決方案:

JSONObject和JSONArray

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM