【发布时间】:2015-12-18 16:03:02
【问题描述】:
好的,这就是我正在使用的一个 API,它对于一个 JSON 参数可以返回两种不同的类型。所以我可以从服务器接收 JSON 对象或字符串。我对 Android 开发还很陌生,所以如果有人可以用代码示例向我解释我如何处理这个问题。
示例 json 响应 {video:"ID OF VIDEO"} 或 {video:{id:"ID OF VIDEO",...extra data}}。我查看了自定义反序列化器,但找不到易于理解的示例。必须有一个简单的方法来解决我的问题。目前我收到错误“预期的字符串,但发现 BEGIN OBJECT”
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class MyNotification {
@SerializedName("_id")
@Expose
private String Id;
@SerializedName("comment")
@Expose
private String comment;
@SerializedName("createdAt")
@Expose
private String createdAt;
@SerializedName("message")
@Expose
private String message;
@SerializedName("read")
@Expose
private Boolean read;
@SerializedName("recipient")
@Expose
private String recipient;
@SerializedName("sender")
@Expose
private User sender;
@SerializedName("type")
@Expose
private String type;
// @SerializedName("video")
// @Expose
// private String video;
/**
*
* @return
* The Id
*/
public String getId() {
return Id;
}
/**
*
* @param Id
* The _id
*/
public void setId(String Id) {
this.Id = Id;
}
/**
*
* @return
* The comment
*/
public String getComment() {
return comment;
}
/**
*
* @param comment
* The comment
*/
public void setComment(String comment) {
this.comment = comment;
}
/**
*
* @return
* The createdAt
*/
public String getCreatedAt() {
return createdAt;
}
/**
*
* @param createdAt
* The createdAt
*/
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
/**
*
* @return
* The message
*/
public String getMessage() {
return message;
}
/**
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}
/**
*
* @return
* The read
*/
public Boolean getRead() {
return read;
}
/**
*
* @param read
* The read
*/
public void setRead(Boolean read) {
this.read = read;
}
/**
*
* @return
* The recipient
*/
public String getRecipient() {
return recipient;
}
/**
*
* @param recipient
* The recipient
*/
public void setRecipient(String recipient) {
this.recipient = recipient;
}
/**
*
* @return
* The sender
*/
public User getSender() {
return sender;
}
/**
*
* @param sender
* The sender
*/
public void setSender(User sender) {
this.sender = sender;
}
/**
*
* @return
* The type
*/
public String getType() {
return type;
}
/**
*
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}
// /**
// *
// * @return
// * The video
// */
// public String getVideo() {
// return video;
// }
//
// /**
// *
// * @param video
// * The video
// */
// public void setVideo(String video) {
// this.video = video;
// }
}
还有那部分的废话
Gson gson = new Gson();
String jsonString = String.valueOf(dataset);
Type listType = new TypeToken<List<MyNotification>>(){}.getType();
notficationsList = (List<MyNotification>) gson.fromJson(jsonString, listType);
【问题讨论】:
-
请发布您的代码
-
发布我的代码?不确定这会有什么帮助?你是说我的 POJO 定义吗?
-
是的,你的 POJO 定义会有所帮助,Java 部分也会抛出异常
-
注释掉给我带来麻烦的部分。我明白如果我将它定义为一个字符串,它在将 JSON 转换为 pojo 时需要一个字符串,但必须有一种方法来处理我不知道的。
-
json 是在哪里创建的?它是您自己的服务还是外部服务?