0
我有一個相對簡單的java對象:Java來JSON轉換拋出意外的標記錯誤
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;
}
}
然後我用Spring踐踏過的WebSocket要發送消息給客戶端:
@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));
}
我知道這是被收到客戶端,我解析它是這樣的:
var obj = JSON.parse(payload);
但我的Chrome開發人員工具控制檯說,否則
<<< 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\"}"
它拋出「未捕獲的SyntaxError:意外的標記U」
感謝您指出我在正確的方向。特別是對於我處理stomp協議消息的情況,json數據存儲爲payload.body所以var obj = JSON.parse(payload.body) – ron