dongmeiran609914 2015-09-28 07:01
浏览 135

如何在Golang中使用jsonrpc包从服务器向客户端发送嵌套地图?

I am writing a client server go application which uses yahoo finance api to fetch the real time stock price. I am using jsonrpc package to connect client and server. I am unable to pass a nested map response from server to client. Here is my small snippet from the client file. client.go file

var (
        reply map[string]map[int]float64
    )

    c := jsonrpc.NewClient(client)
    err = c.Call("JSONResponse.GetStockValue", args, &reply)
    fmt.Println(reply)

Server file looks like this:

func (j *JSONResponse) GetStockValue(args *ClientRequest, reply *map[string]map[int]float64) error {
 some piece of code......
 nestedMap := make(map[string]map[int]float64)
 add some values in nested map .....
 fmt.Println(nestedMap)
 *reply = nestedMap
  return nil
}

This does not send any response to the client. When i change the nested map to simple map like map[string]int, it correctly works. The nested map is correctly displayed at server but does not get displayed on the client side. The client simply keeps on waiting for the response from the server. It would be very helpful if someone could guide me on why it is not accepting a nested map and working fine for a simple map ?

Thanks :)

  • 写回答

2条回答 默认 最新

  • duanchi4544 2015-09-29 01:21
    关注

    jsonrpc is json rpc because it use json for serialization. to Marshal a map to json, you need string key type. refer to encoding/json

    评论

报告相同问题?