【问题标题】:Gson parsing varied data into an objectGson 将不同的数据解析成一个对象
【发布时间】:2015-03-01 13:29:24
【问题描述】:

我有一个不同的数据类型,它作为我通过改造获取的 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 对象。

【问题讨论】:

    标签: java android json gson retrofit


    【解决方案1】:

    例如你有 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;
            }
    

    【讨论】:

    • 感谢您的回答,但我无法推断如何根据该特定类型设置不同的对象。我更新了问题以澄清我的类定义。
    • 我的推断是,如果数据不是 List 对象,而是未预定义的自定义对象,但会改变我将如何解释这一点。
    • 可以设置 List 否?
    • 但困难的部分是,我不知道对象会是什么。它可以是图像或视频,具体取决于类型。我正在考虑创建一个通用类并设置它,但我不太确定。
    • @itsyogesh 如果你尝试我的方法工作,在你得到 contact.getData().get(i) 你检查是视频还是图像..例如 switch(contact.getData().get (i)){ case("video_name"): 做某事 case ("image_name"): 做某事}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    相关资源
    最近更新 更多