英文:
How to get data from big json in go
问题
我有这个JSON,现在我需要获取字段"textValue"的值为["BigFoot Inc."]。
我现在能够获取到的只是自定义数据数组中的数据,但是我无法获取特定字段的数据。我尝试获取数据,但是一切都很糟糕,我很高兴能得到你的帮助。
JSON:
"id": 759,
"author": {
"id": 1,
"name": "Gogi Na Vole",
"completedOn": "Never",
"custom_fields": [
{
"id": 86,
"name": "property_86",
"label": "Type of Question",
"value": [
"90"
],
"textValue": [
"Other"
]
},
{
"id": 69,
"name": "property_69",
"label": "Client",
"value": [
"82"
],
"textValue": [
"BigFoot Inc."
]
}
]
}
}```
我的代码:
```package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
var body = []byte(`JSON HERE
`)
type TiketData struct {
Struct here
}
func main() {
var data TiketData
json_err := json.Unmarshal(body, &data)
if json_err != nil {
fmt.Println(json_err)
}
for _, customFields := range data.CustomFields {
fmt.Println(fmt.Sprintf("%#v", customFields))
}
}
英文:
I have that json and now i need to get that field "textValue": [
"BigFoot Inc."
]
All that I have now been able to get is data from an array of custom data. But I can't get data from a specific field. I tried to get the data, but everything is very sad, I will be glad for your help
JSON:
"id": 759,
"author": {
"id": 1,
"name": "Gogi Na Vole",
"completedOn": "Never",
"custom_fields": [
{
"id": 86,
"name": "property_86",
"label": "Type of Question",
"value": [
"90"
],
"textValue": [
"Other"
]
},
{
"id": 69,
"name": "property_69",
"label": "Client",
"value": [
"82"
],
"textValue": [
"BigFoot Inc."
]
}
]
}
MyCode:
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
var body = []byte(`JSON HERE
`)
type TiketData struct {
Struct here
}
func main() {
var data TiketData
json_err := json.Unmarshal(body, &data)
if json_err != nil {
fmt.Println(json_err)
}
for _, customFields := range data.CustomFields {
fmt.Println(fmt.Sprintf("%#v", customFields))
}
}
答案1
得分: 0
首先,你需要为你的JSON定义struct type
。我使用了自动生成的struct type
,但你也可以将其分成多个部分。然后,只需按照相同的步骤进行解组和访问变量。希望这能解决你的问题。
package main
import (
"encoding/json"
"fmt"
)
var j = `{
"id": 759,
"author": {
"id": 1,
"name": "Gogi Na Vole",
"completedOn": "Never",
"custom_fields": [
{
"id": 86,
"name": "property_86",
"label": "Type of Question",
"value": [
"90"
],
"textValue": [
"Other"
]
},
{
"id": 69,
"name": "property_69",
"label": "Client",
"value": [
"82"
],
"textValue": [
"BigFoot Inc."
]
}
]
}
}`
type Data struct {
Id int `json:"id"`
Author struct {
Id int `json:"id"`
Name string `json:"name"`
CompletedOn string `json:"completedOn"`
CustomFields []struct {
Id int `json:"id"`
Name string `json:"name"`
Label string `json:"label"`
Value []string `json:"value"`
TextValue []string `json:"textValue"`
} `json:"custom_fields"`
} `json:"author"`
}
func main() {
var data *Data
err := json.Unmarshal([]byte(j), &data)
if err != nil {
fmt.Println(err.Error())
return
}
textVal := data.Author.CustomFields[1].TextValue
fmt.Println(textVal)
}
输出将是:
[BigFoot Inc.]
英文:
Firstly, you need to define struct type
for your JSON. I used auto-generated struct type
but you can divide into multiple ones. Then, just apply the same steps for unmarshalling and access the variable. I hope it will solve your problem.
package main
import (
"encoding/json"
"fmt"
)
var j = `{
"id": 759,
"author": {
"id": 1,
"name": "Gogi Na Vole",
"completedOn": "Never",
"custom_fields": [
{
"id": 86,
"name": "property_86",
"label": "Type of Question",
"value": [
"90"
],
"textValue": [
"Other"
]
},
{
"id": 69,
"name": "property_69",
"label": "Client",
"value": [
"82"
],
"textValue": [
"BigFoot Inc."
]
}
]
}
}`
type Data struct {
Id int `json:"id"`
Author struct {
Id int `json:"id"`
Name string `json:"name"`
CompletedOn string `json:"completedOn"`
CustomFields []struct {
Id int `json:"id"`
Name string `json:"name"`
Label string `json:"label"`
Value []string `json:"value"`
TextValue []string `json:"textValue"`
} `json:"custom_fields"`
} `json:"author"`
}
func main() {
var data *Data
err := json.Unmarshal([]byte(j), &data)
if err != nil {
fmt.Println(err.Error())
return
}
textVal := data.Author.CustomFields[1].TextValue
fmt.Println(textVal)
}
Output will be:
[BigFoot Inc.]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论