简体   繁体   English

golang websocket.JSON.Receive无效字符'q'寻找值的开头

[英]golang websocket.JSON.Receive invalid character 'q' looking for beginning of value

ERROR while receiving JSON data via websockets通过 websockets 接收 JSON 数据时出错

invalid character 'q' looking for beginning of value寻找值开头的无效字符“q”

I am using "golang.org/x/net/websocket" package我正在使用“golang.org/x/net/websocket”包

I have a Message struct:我有一个消息结构:

type Message struct {
    Sender     *Client `json:"sender",omitempty`
    Sendername string  `json:"senderName"`
    Message    string  `json:"message"`
}

And the code that i am using for receiving on server is:我用于在服务器上接收的代码是:

var err error

    for {
        reply := Message{}
        if err = websocket.JSON.Receive(ws, &reply); err != nil {
            fmt.Println("Cannot receive " + err.Error())
            break
        }

        fmt.Println(reply)
}

And the JSON string I am passing is我传递的 JSON 字符串是

{"senderName":"CJ","message":"DATA"}

Client side:客户端:

let msg = document.getElementById("message").value             
let obj = { senderName : "CJ", message : msg}             
console.log(JSON.stringify(obj))             
sock.send(msg);

I am not able to figure out the error, I think it is regarding the JSON data.我无法找出错误,我认为它与 JSON 数据有关。

The issue is related to the data sent by the client.该问题与客户端发送的数据有关。 That data is likely not a json data.该数据可能不是 json 数据。

The client is currently sending document.getElementById("message").value which is a string and not a json data.客户端当前正在发送document.getElementById("message").value它是一个字符串而不是一个 json 数据。

The client code should be modified as follows:客户端代码应修改如下:

sock.send(JSON.stringify(obj))

暂无
暂无

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

相关问题 Golang无效字符'b'寻找值的开始 - Golang invalid character 'b' looking for beginning of value “无效字符'u'寻找值的开始”使用golang开发的服务解析错误 - “invalid character 'u' looking for beginning of value”Parsing Error from an Service developed in golang 从精读的HTTP中解组JSON:无效字符寻找值的开头 - Go unmarshalling JSON from compessed HTTP: invalid character looking for beginning of value 解组 JSON 返回错误:无效字符 '\\x1f' 寻找值的开头 - Unmarshalling JSON returns an error: invalid character '\x1f' looking for beginning of value JSON无效字符'}'正在寻找对象键字符串的开头 - JSON invalid character '}' looking for beginning of object key string Parse.com说“寻找值的开头的无效字符'\\” - Parse.com says “invalid character '\'' looking for beginning of value” Xamarin&Golang-{[]}无效字符'\\ x00'寻找对象密钥字符串的开头 - Xamarin & Golang - { []} invalid character '\x00' looking for beginning of object key string json 格式无效,请检查。 原因:寻找对象键字符串开头的无效字符“a” - Invalid json format, please check. Reason: invalid character 'a' looking for beginning of object key string JSON无法写入创世纪块:无效字符'\\\\'寻找对象密钥字符串的开头 - JSON failed to write genesis block: invalid character '\\' looking for beginning of object key string JSON int密钥发布数据e2e(寻找对象密钥字符串开头的无效字符“ 1”) - JSON int key issue data e2e (invalid character '1' looking for beginning of object key string)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM