dreamevil0002
2017-09-26 20:59
浏览 126
已采纳

Golang后端到javascript JSON解析

I am using the gorilla websocket library for golang: http://www.gorillatoolkit.org/pkg/websocket

This is the code I am using to create the websocket connection:

conn, err := upgrader.Upgrade(w, r, nil)

Having an issue with sending JSON from golang to javascript. I can get it working but have to do what seem to be unnecessary steps. Here is the basics of the golang backend that does not work:

    type clientDB struct{
         ChunksWritten int64
         ChunksRead int64
         BytesWritten   int64
         BytesRead    int64
         DataBytesWritten int64
         DataBytesRead    int64
         ActivePeers   int
         TotalPeers    int
         TorrentHashString string
}

fullClientDB := new(clientDB) //creating a new clientDB struct

b, err := json.Marshal(fullClientDB)
        if err != nil {
            fmt.Println(err)
            return
        }


conn.WriteJSON(b)

When I use JSON.parse in javascript I get the following response:

var clientUpdate = JSON.parse(evt.data);

eyJDaHVua3NXcml0dGVuIjowLCJDaHVua3NSZWFkIjowLCJCeXRlc1dyaXR0ZW4iOjU0NDgxLCJCeXRlc1JlYWQiOjc4NzgyLCJEYXRhQnl0ZXNXcml0dGVuIjowLCJEYXRhQnl0ZXNSZWFkIjowLCJBY3RpdmVQZWVycyI6MCwiVG90YWxQZWVycyI6NDMsIlRvcnJlbnRIYXNoU3RyaW5nIjoiOWY5MTY1ZDlhMjgxYTliOGU3ODJjZDUxNzZiYmNjODI1NmZkMTg3MSJ9

I can get it working making the following changes:

conn.WriteJSON(string(b))

Then in javascript I actually parse the data TWICE.

var clientUpdate = JSON.parse(evt.data);
var clientUpdateJSON = JSON.parse(clientUpdate);

After that I can access the data correctly as a JSON object. Is there something I'm missing about sending JSON objects from golang to javascript over websockets?

图片转代码服务由CSDN问答提供 功能建议

我正在使用gorill的gorilla websocket库: http://www.gorillatoolkit.org/pkg/websocket

这是代码 我用来创建websocket连接:

  conn,err:= upgrader.Upgrade(w,r,nil)
   
  
 

从golang向javascript发送JSON时遇到问题。 我可以使其正常运行,但必须执行似乎不必要的步骤。 这是无法使用的golang后端的基础知识:

 类型clientDB struct {
 ChunksWritten int64 
 ChunksRead int64 
 BytesWritten int64 
 BytesRead int64 
 DataBytesWritten  int64 
 DataBytesRead int64 
 ActivePeers int 
 TotalPeers int 
 TorrentHashString string 
} 
 
fullClientDB:= new(clientDB)//创建新的clientDB结构
 
b,err:= json.Marshal(fullClientDB  )
 if err!= nil {
 fmt.Println(err)
 return 
} 
 
 
conn.WriteJSON(b)
   
 
 

当我在javascript中使用JSON.parse时,会收到以下响应:

  var clientUpdate = JSON.parse(evt.data); 
   
 
 

eyJDaHVua3NXcml0dGVuIjowLCJDaHVua3NSZWFkIjowLCJCeXRlc1dyaXR0ZW4iOjU0NDgxLCJCeXRlc1JlYWQiOjc4NzgyLCJEYXRhQnl0ZXNXcml0dGVuIjowLCJEYXRhQnl0ZXNSZWFkIjowLCJBY3RpdmVQZWVycyI6MCwiVG90YWxQZWVycyI6NDMsIlRvcnJlbnRIYXNoU3RyaW5nIjo iOWY5MTY1ZDlhMjgxYTliOGU3ODJjZDUxNzZiYmNjODI1NmZkMTg3MSJ9

我可以通过以下更改使它工作:

  conn.WriteJSON(string(b)  )
   
 
 

然后在javascript中我实际上解析了TWICE数据。

  var clientUpdate = JSON.parse(evt  .data); 
var clientUpdateJSON = JSON.parse(clientUpdate); 
   
 
 

之后,我可以正确地作为JSON对象访问数据。 通过网络套接字将gogo中的JSON对象从javascript发送到javascript,我会缺少什么吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题