duanben4771 2017-08-11 02:49
浏览 51
已采纳

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

I'm working on a project and it is my first time using Go.

The project queries a number of APIs and for the most part I have had no trouble getting this working.

Coming from a PHP background, creating Go type definitions for my JSON responses is a little different.

I am stuck on one API, a Magento API, that returns a JSON response like so:

{
    "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"
    }
}

I have been using a tool, JSON-to-Go, to help me create the struct types, however it doesn't look quite right for this style of response:

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"`
}

All I am interested in is the inner JSON - the stuff to actually do with the customer. I am looping over this to extract some information.

How do I create the required struct to read from this?

I have looked at any number of documents or articles but they tend to use more simple JSON responses as examples.

  • 写回答

3条回答 默认 最新

  • doumou3883 2017-08-11 03:16
    关注

    For your JSON structure following might suit well.

    Play Link: https://play.golang.org/p/ygXsdYALCb

    Create a struct called Info or name you prefer also customize your field names as you like.

    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"`
    }
    

    And create map of Info struct and the unmarshal it.

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

    EDIT:

    As asked in the comment, adding for example:

    fmt.Println("Accessing unmarshal values:")
    for key, info := range result {
        fmt.Println("Key:", key)
        fmt.Printf("Complete Object: %+v
    ", info)
        fmt.Println("Individual value, typical object field access:")
        fmt.Println("EntityID:", info.EntityID)
        fmt.Println("Email:", info.Email)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值