如何使用GSON库解析这个传入的JSON字符串?

vxqlmq5t  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(196)

我有一个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类的结构有问题,但我不确定是什么问题。

编辑日期:________________________________________________

下面是我的Point类:

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字符串时没有出现问题。

omqzjyyz

omqzjyyz1#

使用Gson创建响应模型类,如下所示:
下面是您的WsMessage类,

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;
}

}

这是“您的观点”课程,

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类中任何值,API调用后使用Gson www.example.com打印您WsMessagelib.like:

Log.d(TAG, "onMessage: Msg: " + new Gson().toJson(wsMessage));
umuewwlo

umuewwlo2#

请尝试使用此解析器。

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;
}
r6vfmomb

r6vfmomb3#

经过几个小时的调试......原来是服务器端在JSON对象前面添加了一个string的问题,我在使用log.d输出时没有注意到这个问题。GSON转换json字符串没有问题。

相关问题