FAQ
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

--

Search Discussions

  • DisposaBoy at Dec 24, 2012 at 5:18 pm
    I can't test it but at a glance, your data has key "Key" while your variable uses "G"

    --
  • 赵超 at Dec 25, 2012 at 3:41 am
    you should write data like this:
    data := []byte("{\"G\":{\"Key\": {\"A\":1,\"B\":2}}}")
    and will get output

    &{map[key:{1 2}]}

    but i recommand you to give field an symbol ,for example
    type Param struct {
    A int "a"
    B int "b"
    }

    you will write 'data' like this :
    data := []byte("{\"body\":{\"key\":{\"a\":1,\"b\":2}}}")


    在 2012年12月24日星期一UTC+8下午10时21分58秒,nimish gupta写道:
    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
    --
  • Jason Del Ponte at Dec 25, 2012 at 4:07 am
    Make sure when tagging structure fields for json it follows this pattern:
    `json:"name"`, where name is the field name that exists in the JSON data
    structure.
    http://play.golang.org/p/xDxVtpbYAw

    Using just "name" as a field on a structure won't have any affect.

    On Monday, December 24, 2012 7:41:41 PM UTC-8, 赵超 wrote:

    you should write data like this:
    data := []byte("{\"G\":{\"Key\": {\"A\":1,\"B\":2}}}")
    and will get output

    &{map[key:{1 2}]}

    but i recommand you to give field an symbol ,for example
    type Param struct {
    A int "a"
    B int "b"
    }

    you will write 'data' like this :
    data := []byte("{\"body\":{\"key\":{\"a\":1,\"b\":2}}}")


    在 2012年12月24日星期一UTC+8下午10时21分58秒,nimish gupta写道:
    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
    --
  • 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/yFxVPjeZD3


    If 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_Zv


    Also 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
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 24, '12 at 4:55p
activeDec 25, '12 at 4:07a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase