Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

android - 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:

  {
    "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:

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.

Here is how I'm using the the 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.

Edit:__________________________________________

Here is my 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:__________________________________________________

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. There was no issue with the GSON converting the json string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create your response Model class using Gson like this,

Here is Your 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,

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,

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...