代码之家  ›  专栏  ›  技术社区  ›  James

在Go中创建要从API读取的结构

  •  0
  • James  · 技术社区  · 9 年前

    我正在做一个项目,这是我第一次使用围棋。

    我一直在使用一个API,一个Magento API,它返回一个JSON响应,如下所示:

    {
        "66937": {
            "entity_id": "66937",
            "website_id": "1",
            "email": "email@email.com",
            "group_id": "1",
            "created_at": "2017-08-11 02:09:18",
            "disable_auto_group_change": "0",
            "firstname": "Joe",
            "lastname": "Bloggs",
            "created_in": "New Zealand Store View"
        },
        "66938": {
            "entity_id": "66938",
            "website_id": "1",
            "email": "email1@email.comm",
            "group_id": "1",
            "created_at": "2017-08-11 02:16:41",
            "disable_auto_group_change": "0",
            "firstname": "Jane",
            "lastname": "Doe",
            "created_in": "New Zealand Store View"
        }
    }
    

    我一直在使用工具, JSON-to-Go struct 但是,对于这种类型的响应,它看起来不太合适:

    type AutoGenerated struct {
        Num0 struct {
            EntityID               string `json:"entity_id"`
            WebsiteID              string `json:"website_id"`
            Email                  string `json:"email"`
            GroupID                string `json:"group_id"`
            CreatedAt              string `json:"created_at"`
            DisableAutoGroupChange string `json:"disable_auto_group_change"`
            Firstname              string `json:"firstname"`
            Lastname               string `json:"lastname"`
            CreatedIn              string `json:"created_in"`
        } `json:"0"`
        Num1 struct {
            EntityID               string `json:"entity_id"`
            WebsiteID              string `json:"website_id"`
            Email                  string `json:"email"`
            GroupID                string `json:"group_id"`
            CreatedAt              string `json:"created_at"`
            DisableAutoGroupChange string `json:"disable_auto_group_change"`
            Firstname              string `json:"firstname"`
            Lastname               string `json:"lastname"`
            CreatedIn              string `json:"created_in"`
        } `json:"1"`
    }
    

    结构 从中阅读?

    3 回复  |  直到 9 年前
        1
  •  1
  •   jeevatkm    9 年前

    对于您的JSON结构,以下内容可能很适合。

    播放链接: https://play.golang.org/p/ygXsdYALCb

    struct 打电话 Info 或者您喜欢的名称也可以根据需要自定义字段名称。

    type Info struct {
        EntityID               string `json:"entity_id"`
        WebsiteID              string `json:"website_id"`
        Email                  string `json:"email"`
        GroupID                string `json:"group_id"`
        CreatedAt              string `json:"created_at"`
        DisableAutoGroupChange string `json:"disable_auto_group_change"`
        Firstname              string `json:"firstname"`
        Lastname               string `json:"lastname"`
        CreatedIn              string `json:"created_in"`
    }
    

    并创建 map 信息 结构和解包。

    var result map[string]Info
    if err := json.Unmarshal(jsonBytes, &result); err != nil {
        fmt.Println(err)
    }
    fmt.Printf("%+v", result)
    

    如评论中所问,添加 for 例子:

    fmt.Println("Accessing unmarshal values:")
    for key, info := range result {
        fmt.Println("Key:", key)
        fmt.Printf("Complete Object: %+v\n", info)
        fmt.Println("Individual value, typical object field access:")
        fmt.Println("EntityID:", info.EntityID)
        fmt.Println("Email:", info.Email)
    }
    
        2
  •  1
  •   Xibz    9 年前

    首先,我不喜欢那里自动生成的结构定义。我会把它改成这样

    type Customer struct {
        EntityID               string `json:"entity_id"`
        WebsiteID              string `json:"website_id"`
        Email                  string `json:"email"`
        GroupID                string `json:"group_id"`
        CreatedAt              string `json:"created_at"`
        DisableAutoGroupChange string `json:"disable_auto_group_change"`
        Firstname              string `json:"firstname"`
        Lastname               string `json:"lastname"`
        CreatedIn              string `json:"created_in"`
    }
    

    type Customers map[string]Customer
    

    customers := Customers{}
    err := json.Unmarshal(jsonBytes, &customers)
    
        3
  •  0
  •   user3555216 user3555216    9 年前

    @Xibz和@jeevatkm都提供了很好的解决方案。然而,在某些情况下,并非所有JSON结构都可以解组为Go结构。您可能需要定义自己的解码函数。

    如果您必须为特定的数据类型或结构定义自己的解码函数,您也可以尝试gorilla的模式包。

    https://github.com/gorilla/schema