ron
ron

Reputation: 229

Java to JSON conversion throws unexpected token error

I have a relatively simple java object:

public class TestEvent {
    private String time;
    private String value;

    public TestEvent(){};

    public TestEvent(String time, String value)
    {
      this.time = time;
      this.value = value;
    }

    public String getTime() {
       return time;
    }

    public String setTime(String time) {
        this.time = time;
    }
}

Then I use Spring Stomp over Websocket to send message to client:

@Autowired
private SimpMessagingTemplate template;

private static Gson gson = new Gson();
private static Type type = new TypeToken<RttEvent>() {}.getType();

public void Test() {
    this.template.convertAndSend("/topic/123", gson.toJson(event, type));
}

I know it is being received on the client side and I parse it like this:

var obj = JSON.parse(payload);

But my Chrome developer tool console says otherwise

<<< MESSAGE
expires:0
destination:/topic/123
subscription:sub-0
priority:4
message-id:ID\cPC78945-52231-1456805172516-3\c1\c-1\c1\c54
content-type:application/json;charset=UTF-8
timestamp:1456805443802
content-length:53

"{\"time\":\"2016-01-02\",\"value\":\"-1855286068\"}"

It throws "Uncaught SyntaxError: unexpected token u"

Upvotes: 0

Views: 687

Answers (1)

Tah
Tah

Reputation: 1536

It looks like the data is in the content of your response. You need to change

var obj = JSON.parse(payload);

to

var obj = JSON.parse(payload.body);

Upvotes: 1

Related Questions