方法一 :在后台需要转换String - json
let param = new URLSearchParams();
param.append(‘username‘, this.username);
param.append(‘content‘, this.inputValue);
this.socket.send(param ); //向后台发信息
方法二(推荐)
let row={};
row[‘key‘] = ‘value‘;
row[‘key2‘] = ‘value2‘;
row[‘key3‘] = ‘value3‘;
this.socket.send(JSON.stringify(row));//向后台发信息
后台处理JSON
String 转 JSON 对象
方法:
JSON.parseObject(Srting,Class);
例子:
ChatMessage j = JSON.parseObject(message.getPayload().toString(), ChatMessage.class);