R
R
Roman Rakzin2015-04-02 09:44:45
go
Roman Rakzin, 2015-04-02 09:44:45

How to parse json in go?

Good afternoon,
How to parse json in go?
There is the following code
type testRecord struct {
To_socket []string `json:"to_socket"`
Message []string `json:"message"`
}
var ans testRecord
b := []byte(`{"to_socket":"11", "message":"message_texttt"}`) //This is what I get from the client
err = json.Unmarshal(b, &ans)
// log.Printf("%#v", ans)
// log.Printf("To_socket value =" +ans.To_socket) // I want to get the value here
Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2015-04-02
@TwoRS

You have 2 errors:
1. err :=, instead of err =
2. To_socket []string `json:"to_socket"` - here you explicitly stated that To_socket is an array of strings, but in fact you are passing a string.
Working example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question