我有那個 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."
]
}
]
}
我的代碼:
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))
}
}
uj5u.com熱心網友回復:
首先,您需要struct type為您的 JSON定義。我使用了自動生成,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.]
去游樂場
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358285.html
