简体   繁体   English

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

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

I am trying to convert a complex object into related Java beans. 我正在尝试将复杂的对象转换为相关的Java Bean。 However, some sub class can be not generated correctly. 但是,某些子类可能无法正确生成。

I am just using simulate MS Adaptive card to create a set of Java beans classes. 我只是使用模拟MS Adaptive卡来创建一组Java Bean类。 When I call Gson package or Alibaba fastJson package to parse my json data. 当我调用Gson包或阿里巴巴fastJson包来解析我的json数据时。 it always shows the super class type. 它始终显示超类类型。

This is just an experiment to test Gson & fastJson whether it can convert complex objects. 这只是测试Gson和fastJson是否可以转换复杂对象的实验。 which is running on the Android studio. 在Android Studio上运行。

My demo json is like following: 我的演示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"
    }]
}

Therefore, I just follow MS adaptive card to create following java beans. 因此,我只是按照MS自适应卡来创建以下Java Bean。

First class: AdaptiveTypedElement 头等舱: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;

}

Second class: AdaptiveTypedElement 第二类: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;

}

Third class AdaptiveContainer: 第三类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;
}

In the end, I finally got AdaptiveCard Object. 最后,我终于得到了AdaptiveCard对象。 See my code: 看我的代码:

return JSON.parseObject(attachJson, AdaptiveCard.class) . return JSON.parseObject(attachJson, AdaptiveCard.class) Note that I add "implementation 'com.alibaba:fastjson:1.2.54'" in my Android Studio 请注意,我在Android Studio中添加了“实现'com.alibaba:fastjson:1.2.54'”

When I check this object, I found the Body data member should be class "AdaptiveContainer " not "AdaptiveElement". 当我检查该对象时,我发现Body数据成员应该是“ AdaptiveContainer”类而不是“ AdaptiveElement”。 I was wondering why it did not following subclass mechanism of OOP & OOD. 我想知道为什么它没有遵循OOP和OOD的子类机制。 I am expecting "AdaptiveContainer", but actually output is "AdaptiveElement". 我期待“ AdaptiveContainer”,但实际上输出是“ AdaptiveElement”。

enter image description here 在此处输入图片说明

rib-pet, your question has a far to big example. rib-pet,您的问题有一个很大的例子。 Anyway I tried to create a java class mapping for your sample using GSON 无论如何,我尝试使用GSON为您的示例创建Java类映射

Action.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 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 (dependend on your wished model you should integrate this class better in some existing 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 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 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 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 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 also here better integration needed 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;
}

}

hope it helps! 希望能帮助到你!

I've figured out this solution via the following solution: 我已经通过以下解决方案找到了解决方案:

JSONObject and JSONArray 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