英文:
GO parse nested json array
问题
以下是翻译好的内容:
type response struct {
META struct {
LV []struct {
RESPONSE struct {
Data []struct {
array []struct {
val []string
}
} `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
我该如何获取以下内容中的值?
"data": [
["1", "2", "3"]
]
我尝试过使用接口和结构体。使用接口的结果是接口类型的[1 2 3]
,但我不确定如何获取这些值。当使用结构体时,尝试映射一个数组的数组时遇到了问题,错误信息如下:
"cannot unmarshal array into Go struct field .data of type struct {
vals []string }"
英文:
{
"meta": {
"type": "RESPONSE",
"application": "",
"data0": {
some data
},
"lv1": [
{
"status": "SUCCESS",
"response-type": "JSON",
"message": {},
"response": {
"more_data": "TRUE",
"no_result": "5",
"current_page": "1",
"data": [[
"1",
"2",
"3"]]
}
}
]
}
}
type response struct {
META struct {
LV []struct {
RESPONSE struct {
Data []struct {
array []struct {
val []string
}
} `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
How can I get the values in the following?
"data": [[
"1",
"2",
"3"]]
I've tried both interface and struct. Using interface results in [1 2 3]
of interface type and I'm not sure how I can get the values. When using struct, I ran into problem when trying to map an array of array with error message:
> "cannot unmarshal array into Go struct field .data of type struct {
> vals []string }"
答案1
得分: 14
这是一个字符串的数组的数组,而不是包含字符串数组的结构体的数组,所以你可能更希望像这样定义:
type response struct {
Meta struct {
Lv []struct {
Response struct {
Data [][]string `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
(我还将全大写的字段名更改为符合 Go 代码风格的样式。)
值得一提的是,有一个方便的 JSON 转 Go 工具 这里,在删除 some data
部分(这使得 JSON 无效)后,它为你的输入生成了以下代码:
type AutoGenerated struct {
Meta struct {
Type string `json:"type"`
Application string `json:"application"`
Data0 struct {
} `json:"data0"`
Lv1 []struct {
Status string `json:"status"`
ResponseType string `json:"response-type"`
Message struct {
} `json:"message"`
Response struct {
MoreData string `json:"more_data"`
NoResult string `json:"no_result"`
CurrentPage string `json:"current_page"`
Data [][]string `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
英文:
It's an array of arrays of strings, not an array of structs containing arrays of structs containing strings, so you want something more like:
type response struct {
Meta struct {
Lv []struct {
Response struct {
Data [][]string `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
(I also changed the all-caps field names to match expected Go code style.)
For what it's worth, there is a handy JSON-to-Go tool here which gave me this for your input, after deleting the some data
bit (which made the JSON invalid):
type AutoGenerated struct {
Meta struct {
Type string `json:"type"`
Application string `json:"application"`
Data0 struct {
} `json:"data0"`
Lv1 []struct {
Status string `json:"status"`
ResponseType string `json:"response-type"`
Message struct {
} `json:"message"`
Response struct {
MoreData string `json:"more_data"`
NoResult string `json:"no_result"`
CurrentPage string `json:"current_page"`
Data [][]string `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论