简体   繁体   English

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

[英]How would I parse this incoming JSON string using the GSON library?

I have a websocket server that is sending a JSON string to the users:我有一个 websocket 服务器正在向用户发送 JSON 字符串:

  {
    "message_type" : "draw",
    "point": {
      "color" : "#3b7bbf",
      "width" : 20,
      "x" : 191.98608,
      "y" : 891.96094
    },
    "sender_id" : "123456abc"
  }

I've created a Java Object to use for GSON to parse:我创建了一个 Java Object 用于 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);
    }
}

However it's still giving me a com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ error on an incoming message. However it's still giving me a com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ error on an incoming message.

Here is how I'm using the the GSON:以下是我使用 GSON 的方法:

WsMessage wsMessage = gson.fromJson(text, WsMessage.class);
Log.d(TAG, "onMessage: Msg: " + wsMessage.toString());

I know it's something wrong with the way my WsMessage class is structured, but i'm unsure what.我知道我的WsMessage class 的结构方式有问题,但我不确定是什么。

Edit:__________________________________________编辑:__________________________________________

Here is my Point 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);
    }
}

Edit 2:__________________________________________________编辑 2:__________________________________________________

After many hours of debugging... it turned out to be an issue from the server side adding a string in front of the JSON object and I didn't notice it because I was printing it out using log.d.经过数小时的调试......原来是服务器端的问题,在 JSON object 前面添加了一个字符串,我没有注意到它,因为我是使用 log.d 打印出来的。 There was no issue with the GSON converting the json string. GSON 转换 json 字符串没有问题。

Create your response Model class using Gson like this,像这样使用 Gson 创建您的响应 Model class,

Here is Your WsMessage 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;
}

}

Here is Your Point class,这是您的观点 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;
}

}

Now you can parse any value from WsMessage class,After API calling print your WsMessage Using Gson lib.like this,现在您可以解析 WsMessage class 中的任何值,在 API 调用使用 Gson lib.like 打印您的 WsMessage 之后,

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

Try this parser.试试这个解析器。

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

After many hours of debugging... it turned out to be an issue from the server side adding a string in front of the JSON object and I didn't notice it when printing it out using log.d .经过数小时的调试......原来是服务器端在 JSON object 前面添加一个string的问题,我在使用log.d打印出来时没有注意到它。 There was no issue with the GSON converting the json string. GSON 转换 json 字符串没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM