英文:
Golang how to fetch specific key (from multistructure) from the rest api json response
问题
所需的JSON特定键是:status -> name
和id
的值
getUrl := "https://www.test.com"
reqTransitionID, err := http.NewRequest("GET", getUrl, nil)
respTransitionID, err := client.Do(reqTransitionID)
if err != nil {
log.Fatal(err)
}
defer respTransitionID.Body.Close()
type Statuskey struct {
Id string `json:"id"`
Name string `json:"name"`
}
type TransitionID struct {
Status Statuskey `json:"status"`
}
jsonData, errrr := ioutil.ReadAll(respTransitionID.Body)
if errrr != nil {
fmt.Println("Error reading JSON data:", errrr)
return
}
fmt.Println(string(jsonData))
var ss TransitionID
json.Unmarshal(jsonData, &ss)
fmt.Println(ss.Status)
fmt.Println(ss.Status.Id)
fmt.Println(ss.Status.Name)
我的JSON响应是:
{
"expand":"demo",
"id":"825",
"self":"TEST",
"key":"TEST",
"fields":{
"status":{
"self":"tst",
"description":"test",
"iconUrl":"test",
"name":"Closed",
"id":"6",
"statusCategory":{
"self":"test",
"id":3,
"key":"done",
"colorName":"green",
"name":"Done"
}
}
}
}
英文:
Required json specific key: status -> name
and id
values only
getUrl := "https://www.test.com"
reqTransitionID, err := http.NewRequest("GET", getUrl, nil)
respTransitionID, err := client.Do(reqTransitionID)
if err != nil {
log.Fatal(err)
}
defer respTransitionID.Body.Close()
type Statuskey struct {
Id string `json:"id"`
Name string `json:"name"`
}
type TransitionID struct {
Status Statuskey `json:"status"`
}
jsonData, errrr := ioutil.ReadAll(respTransitionID.Body)
if errrr != nil {
fmt.Println("Error reading JSON data:", errrr)
return
}
fmt.Println(string(jsonData))
var ss TransitionID
json.Unmarshal(jsonData, &ss)
fmt.Println(ss.Status)
fmt.Println(ss.Status.Id)
fmt.Println(ss.Status.Name)
My Json response is :
{
"expand":"demo",
"id":"825",
"self":"TEST",
"key":"TEST",
"fields":{
"status":{
"self":"tst",
"description":"test",
"iconUrl":"test",
"name":"Closed",
"id":"6",
"statusCategory":{
"self":"test",
"id":3,
"key":"done",
"colorName":"green",
"name":"Done"
}
}
}
}
答案1
得分: 1
jsonDataSrc, err := ioutil.ReadAll(respTransitionID.Body)
if err != nil {
fmt.Println("读取JSON数据时出错:", err)
return
}
type Status struct {
Name string json:"name"
Id string json:"id"
}
type Fields struct {
Status Status json:"status"
}
type JsonResponse struct {
Fields Fields json:"fields"
}
res := JsonResponse{}
json.Unmarshal([]byte(jsonDataSrc), &res)
fmt.Println(res.Fields.Status.Id)
fmt.Println(res.Fields.Status.Name)
输出结果:
6
Closed
英文:
jsonDataSrc, errrr := ioutil.ReadAll(respTransitionID.Body)
if errrr != nil {
fmt.Println("Error reading JSON data:", errrr)
return
}
type Status struct {
Name string `json:"name"`
Id string `json:"id"`
}
type Fields struct {
Status Status `json:"status"`
}
type JsonResponse struct {
Fields Fields `json:"fields"`
}
res := JsonResponse{}
json.Unmarshal([]byte(jsonDataSrc), &res)
fmt.Println(res.Fields.Status.Id)
fmt.Println(res.Fields.Status.Name)
output - 6
Closed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论