Json 在Go中创建要从API读取的结构
我正在做一个项目,这是我第一次使用围棋 该项目查询了大量的API,在大多数情况下,我都没有遇到任何问题 来自PHP背景,为我的JSON响应创建Go类型定义有点不同 我被困在一个API上,一个Magento API,它返回一个JSON响应,如下所示:Json 在Go中创建要从API读取的结构,json,go,struct,Json,Go,Struct,我正在做一个项目,这是我第一次使用围棋 该项目查询了大量的API,在大多数情况下,我都没有遇到任何问题 来自PHP背景,为我的JSON响应创建Go类型定义有点不同 我被困在一个API上,一个Magento API,它返回一个JSON响应,如下所示: { "66937": { "entity_id": "66937", "website_id": "1", "email": "email@email.com", "group_
{
"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"
}
}
我一直在使用一个工具来帮助我创建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"`
}
我所感兴趣的是内部JSON——与客户实际相关的东西。我在这里循环提取一些信息
如何创建从中读取所需的结构?
我看过很多文档或文章,但它们倾向于使用更简单的JSON响应作为示例。对于您的JSON结构,以下内容可能很适合 播放链接: 创建名为
Info
的struct
或您喜欢的名称,还可以根据需要自定义字段名称
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
struct的map
,并将其解组
var result map[string]Info
if err := json.Unmarshal(jsonBytes, &result); err != nil {
fmt.Println(err)
}
fmt.Printf("%+v", result)
编辑: 如评论中所述,为示例添加
:
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)
}
对于您的JSON结构,以下内容可能非常适合
播放链接:
创建名为Info
的struct
或您喜欢的名称,还可以根据需要自定义字段名称
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
struct的map
,并将其解组
var result map[string]Info
if err := json.Unmarshal(jsonBytes, &result); err != nil {
fmt.Println(err)
}
fmt.Printf("%+v", result)
编辑:
如评论中所述,为
示例添加:
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)
}
首先,我不喜欢自动生成的结构定义。我会把它改成这样
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)
首先,我不喜欢自动生成的结构定义。我会把它改成这样
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)
@Xibz和@jeevatkm都提供了很好的解决方案。但是,在某些情况下,并非所有JSON结构都可以解组为Go结构。您可能需要定义自己的解码功能
如果您必须为特定的数据类型或结构定义自己的解码函数,您也可以尝试gorilla的模式包
@Xibz和@jeevatkm都提供了很好的解决方案。但是,在某些情况下,并非所有JSON结构都可以解组为Go结构。您可能需要定义自己的解码功能
如果您必须为特定的数据类型或结构定义自己的解码函数,您也可以尝试gorilla的模式包
@James在答案中添加了循环细节。“请看一下。”詹姆斯在回答中添加了循环详细信息。请看一看。