Golang如何从REST API的JSON响应中获取特定键(来自多层结构)

huangapple go评论77阅读模式
英文:

Golang how to fetch specific key (from multistructure) from the rest api json response

问题

所需的JSON特定键是:status -> nameid的值

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

huangapple
  • 本文由 发表于 2016年12月22日 00:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/41267046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定