|
Jason Del Ponte |
at Dec 25, 2012 at 4:04 am
|
⇧ |
| |
nimish, it looks like the issue you're facing is because your JSON string
is a map of item. {Key: {A:1,B:2}}, but you are passing in a structure
which contains a map within an internal value, which is a map.
The JSON object that your data structure is able to parse is: {G: {Key:
{A:1,B:2}}. This is because of the 'G' value on the ff
structure.
http://play.golang.org/p/yFxVPjeZD3If you are wanting a structure that will parse just the {Key: {A:1,B:2}}
use a map of string and fg.
http://play.golang.org/p/mncMi1e_ZvAlso I'd suggest you use tagging for your structure fields.
http://golang.org/pkg/encoding/json/#Marshal near the end of
the marshaling section it describes how tagging works with json.
On Monday, December 24, 2012 6:21:58 AM UTC-8, nimish gupta wrote:
Hi,
I am beginner in go. I am trying to do json unmarshal into go lang map.
package main
import "fmt"
import "encoding/json"
type fg struct {
A int
B int
}
type ff struct {
G map[string]fg
}
func main() {
data := []byte("{\"Key\": {\"A\":1,\"B\":2}}")
Rt := &ff{}
err := json.Unmarshal(data, Rt)
if err != nil {
fmt.Println("Error in unmarshalling")
}
fmt.Println(Rt)
}
In the output, the map is nil.
Can some one please help me to figure out what is wrong is my code.
Thanks,
Nimish
--