我将如何使用 GSON 库解析这个传入的 JSON 字符串?
How would I parse this incoming JSON string using the GSON library?
我有一个 websocket 服务器正在向用户发送 JSON 字符串:
{
"message_type" : "draw",
"point": {
"color" : "#3b7bbf",
"width" : 20,
"x" : 191.98608,
"y" : 891.96094
},
"sender_id" : "123456abc"
}
我创建了一个 Java 对象供 GSON 解析:
public class WsMessage {
private String message_type;
private String sender_id;
private Point point;
public WsMessage(String message_type, String sender_id, Point point) {
this.message_type = message_type;
this.sender_id = sender_id;
this.point = point;
}
public String getMessage_type() {
return message_type;
}
public String getSender_id() {
return sender_id;
}
public Point getPoint() {
return point;
}
@NonNull
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, WsMessage.class);
}
}
但是它仍然给我一个传入消息的 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
错误。
这是我使用 GSON 的方式:
WsMessage wsMessage = gson.fromJson(text, WsMessage.class);
Log.d(TAG, "onMessage: Msg: " + wsMessage.toString());
我知道我的 WsMessage
class 的结构有问题,但我不确定是什么。
编辑:__________________________________________
这是我的 Point
class:
public class Point {
private float x;
private float y;
private String color;
private int width;
private Point() {
}
public Point(float x, float y, String color, int width) {
this.x = x;
this.y = y;
this.color = color;
this.width = width;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public String getColor() {
return color;
}
public int getWidth() {
return width;
}
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, Point.class);
}
}
编辑2:__________________________________________________
经过几个小时的调试...原来是服务器端在 JSON 对象前面添加一个字符串的问题,我没有注意到它,因为我正在打印它使用 log.d。 GSON 转换 json 字符串没有问题。
像这样使用 Gson 创建您的响应模型 class,
这是您的 WsMessage Class,
public class WsMessage {
@SerializedName("message_type")
@Expose
private String messageType;
@SerializedName("point")
@Expose
private Point point;
@SerializedName("sender_id")
@Expose
private String senderId;
public WsMessage(String messageType, Point point, String senderId) {
this.messageType = messageType;
this.point = point;
this.senderId = senderId;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public String getSenderId() {
return senderId;
}
public void setSenderId(String senderId) {
this.senderId = senderId;
}
}
这是你的观点class,
public class Point {
@SerializedName("color")
@Expose
private String color;
@SerializedName("width")
@Expose
private Integer width;
@SerializedName("x")
@Expose
private Double x;
@SerializedName("y")
@Expose
private Double y;
public Point(String color, Integer width, Double x, Double y) {
this.color = color;
this.width = width;
this.x = x;
this.y = y;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
public Double getY() {
return y;
}
public void setY(Double y) {
this.y = y;
}
}
现在您可以解析 WsMessage class 中的任何值,在 API 调用打印您的 WsMessage 使用 Gson lib.like 之后,
Log.d(TAG, "onMessage: Msg: " + new Gson().toJson(wsMessage));
试试这个解析器。
public WsMessage parseWsMessage(String json) {
WsMessage result = null;
try {
Type itemsMapType = new TypeToken<WsMessage>() {
}.getType();
result = new Gson().fromJson(json, itemsMapType);
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
经过几个小时的调试...原来是服务器端的问题,在JSON对象前面添加了一个string
,我在打印时没有注意到它它使用 log.d
。 GSON 转换 json 字符串没有问题。
我有一个 websocket 服务器正在向用户发送 JSON 字符串:
{
"message_type" : "draw",
"point": {
"color" : "#3b7bbf",
"width" : 20,
"x" : 191.98608,
"y" : 891.96094
},
"sender_id" : "123456abc"
}
我创建了一个 Java 对象供 GSON 解析:
public class WsMessage {
private String message_type;
private String sender_id;
private Point point;
public WsMessage(String message_type, String sender_id, Point point) {
this.message_type = message_type;
this.sender_id = sender_id;
this.point = point;
}
public String getMessage_type() {
return message_type;
}
public String getSender_id() {
return sender_id;
}
public Point getPoint() {
return point;
}
@NonNull
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, WsMessage.class);
}
}
但是它仍然给我一个传入消息的 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
错误。
这是我使用 GSON 的方式:
WsMessage wsMessage = gson.fromJson(text, WsMessage.class);
Log.d(TAG, "onMessage: Msg: " + wsMessage.toString());
我知道我的 WsMessage
class 的结构有问题,但我不确定是什么。
编辑:__________________________________________
这是我的 Point
class:
public class Point {
private float x;
private float y;
private String color;
private int width;
private Point() {
}
public Point(float x, float y, String color, int width) {
this.x = x;
this.y = y;
this.color = color;
this.width = width;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public String getColor() {
return color;
}
public int getWidth() {
return width;
}
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, Point.class);
}
}
编辑2:__________________________________________________
经过几个小时的调试...原来是服务器端在 JSON 对象前面添加一个字符串的问题,我没有注意到它,因为我正在打印它使用 log.d。 GSON 转换 json 字符串没有问题。
像这样使用 Gson 创建您的响应模型 class,
这是您的 WsMessage Class,
public class WsMessage {
@SerializedName("message_type")
@Expose
private String messageType;
@SerializedName("point")
@Expose
private Point point;
@SerializedName("sender_id")
@Expose
private String senderId;
public WsMessage(String messageType, Point point, String senderId) {
this.messageType = messageType;
this.point = point;
this.senderId = senderId;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public String getSenderId() {
return senderId;
}
public void setSenderId(String senderId) {
this.senderId = senderId;
}
}
这是你的观点class,
public class Point {
@SerializedName("color")
@Expose
private String color;
@SerializedName("width")
@Expose
private Integer width;
@SerializedName("x")
@Expose
private Double x;
@SerializedName("y")
@Expose
private Double y;
public Point(String color, Integer width, Double x, Double y) {
this.color = color;
this.width = width;
this.x = x;
this.y = y;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
public Double getY() {
return y;
}
public void setY(Double y) {
this.y = y;
}
}
现在您可以解析 WsMessage class 中的任何值,在 API 调用打印您的 WsMessage 使用 Gson lib.like 之后,
Log.d(TAG, "onMessage: Msg: " + new Gson().toJson(wsMessage));
试试这个解析器。
public WsMessage parseWsMessage(String json) {
WsMessage result = null;
try {
Type itemsMapType = new TypeToken<WsMessage>() {
}.getType();
result = new Gson().fromJson(json, itemsMapType);
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
经过几个小时的调试...原来是服务器端的问题,在JSON对象前面添加了一个string
,我在打印时没有注意到它它使用 log.d
。 GSON 转换 json 字符串没有问题。