java - Gson将各种数据解析为对象

标签 java android json gson retrofit

我有多种数据类型,作为 json,我通过改造获取它。这是当消息类型是图像时的样子

{
    "_id": ObjectId("54cd78c79ce8756749a8e38c"),
    "data": {
        "image_name": "10347646_865379743494769_4408387832275449094_n.png",
        "image_url": "https://s3-ap-southeast-1.amazonaws.com/clan/e492006e561fe3811e3e16c85d384cf6.png",
        "image_size": 76586
    },
    "receiver_id": "54cd2fb7847828d73ce9047d",
    "sender_id": ObjectId("54cd4354bb8c1b2540bad504"),
    "server_recieved_at": ISODate("2015-02-01T00:52:23.676Z"),
    "sent_at": ISODate("2015-02-01T00:52:23.676Z"),
    "created_at": ISODate("2015-02-01T00:52:23.676Z"),
    "needs_push": true,
    "type": "image",
    "status": "sent",
    "__v": 0
}

消息类型可以不同,可以是视频、图像或文本。对于视频,数据如下所示:

"data": {
        "video_name": "aa_n.mp4",
        "video_url": "https://s3-ap-southeast-1.amazonaws.com/clan/aa_n.png",
        "video_size": 76586
    },

消息类型在 json 中指定,它告诉存在什么类型的数据。

如何在 GSON 中反序列化这些数据。我创建了一个通用的 MessageData 类以及视频和图像类。但我无法理解用 gson 设置数据。

更新问题。这是我的消息类。

@Parcel
public class Message {

    @SerializedName("sender_id")
    private String senderId;

    @SerializedName("receiver_id")
    private String receiverId;

    @SerializedName("status")
    private String status;

    @SerializedName("type")
    private String type;

    @SerializedName("read_status")
    private boolean readStatus;

    @SerializedName("text")
    private String text;

    @SerializedName("needs_push")
    private boolean needsPush;

    //TODO Need to set the data variable


    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getReceiverId() {
        return receiverId;
    }

    public void setReceiverId(String receiverId) {
        this.receiverId = receiverId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getType() {
        return type;
    }

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

    public boolean isReadStatus() {
        return readStatus;
    }

    public void setReadStatus(boolean readStatus) {
        this.readStatus = readStatus;
    }

    public String getText() {
        return text;
    }

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

    public boolean isNeedsPush() {
        return needsPush;
    }

    public void setNeedsPush(boolean needsPush) {
        this.needsPush = needsPush;
    }

我创建了图像和视频类。

@Parcel
public class Image {

    @SerializedName("image_size")
    private Double imageSize;

    @SerializedName("image_url")
    private String imageUrl;

    @SerializedName("image_name")
    private String imageName;
}

根据 json 对象,消息数据对象会有所不同,可以是图像或视频。如何根据类型将其序列化为 gson 对象。

最佳答案

例如,您有 JSONArray 要使用 GSON 进行解析,因此请执行以下操作:

     for(int i=1;i < array.length();i++) {
            Gson gson = new Gson();
            MessageData contact = gson.fromJson(String.valueOf(array.getJSONObject(i)), MessageData.class);

//UPDATE:         
       for(int i=0;i<=contact.getData().size()-1;i++){  
        System.out.println(contact.getData().get(i));  
       }  
        //Here take the data with contact.getTitle() ecc...
        }

还有 MessageData.class(用你的属性更改属性):

 public class MessageData {
//UPDATE
        @SerializedName("data")
        private List<string> data;  

        @SerializedName("url")
        private String url;

        @SerializedName("title")

        private String title;

        @SerializedName("score")
        private String score;

        @SerializedName("source")
        private String source;



 public List<string> getData() {  
  return data;  
 }  
 public void setData(List<string> data) {  
  this.data = data;  
 }  
        public String getTitle() {
            return title;
        }

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

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getScore() {
            return score;
        }

        public void setScore(String score) {
            this.score = score;
        }

        public String getSource() {
            return source;
        }

关于java - Gson将各种数据解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28794073/

相关文章:

java - sqlite查询当前时间和时间戳

java - 使用ListAdapter时如何使ListView对新数据无效

java - 使用 JSON 对象数组执行 Post 时出现 MessageBodyProviderNotFoundException

java - 该 json 的哪个销售点属性?

javascript - 谷歌图表 - "this.ug is not a function"

java - SimpleXML,将具有动态名称的子项转换为列表

java - 从过滤器中的数据库对用户进行身份验证是一种好习惯吗?

android - 无法在 Jitpack 上发布库

android - RecyclerView 填充 LinearLayout 内的空白空间

php - 如何检查文件完整性