2017-08-11 29 views
0

我正在开发一个项目,这是我第一次使用Go。在Go中创建结构以从API中读取

该项目查询了大量的API,并在大多数情况下,我没有得到这个工作的麻烦。

从PHP背景来看,为我的JSON响应创建Go类型定义有点不同。

我被困在一个API,Magento的API,它返回像这样的JSON响应:

{ 
    "66937": { 
     "entity_id": "66937", 
     "website_id": "1", 
     "email": "[email protected]", 
     "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": "[email protected]", 
     "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类型,但是它不“T看起来很适合这种风格的响应:

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

所有我感兴趣的是内JSON - 东西实际上与客户做。我正在循环这个来提取一些信息。

如何创建所需的struct以读取此?

我看过任何数量的文档或文章,但他们倾向于使用更简单的JSON响应作为示例。

回答

1

对于你的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"` 
} 

,创造Info结构和数据编它的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) 
} 
+0

@詹姆斯加入了答案循环细节。请看一看。 – jeevatkm

2

嗯,首先,我不喜欢自动生成的结构定义那里。我想改变这种状况看起来像这样

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 

这应该与你的JSON您提供工作。把它放在一起

customers := Customers{} 
err := json.Unmarshal(jsonBytes, &customers) 
0

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

您也可以尝试大猩猩的架构包,如果你要定义特定的数据类型或结构自己的解码功能。

https://github.com/gorilla/schema