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

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

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

问题

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

  1. getUrl := "https://www.test.com"
  2. reqTransitionID, err := http.NewRequest("GET", getUrl, nil)
  3. respTransitionID, err := client.Do(reqTransitionID)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. defer respTransitionID.Body.Close()
  8. type Statuskey struct {
  9. Id string `json:"id"`
  10. Name string `json:"name"`
  11. }
  12. type TransitionID struct {
  13. Status Statuskey `json:"status"`
  14. }
  15. jsonData, errrr := ioutil.ReadAll(respTransitionID.Body)
  16. if errrr != nil {
  17. fmt.Println("Error reading JSON data:", errrr)
  18. return
  19. }
  20. fmt.Println(string(jsonData))
  21. var ss TransitionID
  22. json.Unmarshal(jsonData, &ss)
  23. fmt.Println(ss.Status)
  24. fmt.Println(ss.Status.Id)
  25. fmt.Println(ss.Status.Name)

我的JSON响应是:

  1. {
  2. "expand":"demo",
  3. "id":"825",
  4. "self":"TEST",
  5. "key":"TEST",
  6. "fields":{
  7. "status":{
  8. "self":"tst",
  9. "description":"test",
  10. "iconUrl":"test",
  11. "name":"Closed",
  12. "id":"6",
  13. "statusCategory":{
  14. "self":"test",
  15. "id":3,
  16. "key":"done",
  17. "colorName":"green",
  18. "name":"Done"
  19. }
  20. }
  21. }
  22. }
英文:

Required json specific key: status -> name and id values only

  1. getUrl := "https://www.test.com"
  2. reqTransitionID, err := http.NewRequest("GET", getUrl, nil)
  3. respTransitionID, err := client.Do(reqTransitionID)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. defer respTransitionID.Body.Close()
  8. type Statuskey struct {
  9. Id string `json:"id"`
  10. Name string `json:"name"`
  11. }
  12. type TransitionID struct {
  13. Status Statuskey `json:"status"`
  14. }
  15. jsonData, errrr := ioutil.ReadAll(respTransitionID.Body)
  16. if errrr != nil {
  17. fmt.Println("Error reading JSON data:", errrr)
  18. return
  19. }
  20. fmt.Println(string(jsonData))
  21. var ss TransitionID
  22. json.Unmarshal(jsonData, &ss)
  23. fmt.Println(ss.Status)
  24. fmt.Println(ss.Status.Id)
  25. fmt.Println(ss.Status.Name)

My Json response is :

  1. {
  2. "expand":"demo",
  3. "id":"825",
  4. "self":"TEST",
  5. "key":"TEST",
  6. "fields":{
  7. "status":{
  8. "self":"tst",
  9. "description":"test",
  10. "iconUrl":"test",
  11. "name":"Closed",
  12. "id":"6",
  13. "statusCategory":{
  14. "self":"test",
  15. "id":3,
  16. "key":"done",
  17. "colorName":"green",
  18. "name":"Done"
  19. }
  20. }
  21. }
  22. }

答案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

英文:
  1. jsonDataSrc, errrr := ioutil.ReadAll(respTransitionID.Body)
  2. if errrr != nil {
  3. fmt.Println("Error reading JSON data:", errrr)
  4. return
  5. }
  6. type Status struct {
  7. Name string `json:"name"`
  8. Id string `json:"id"`
  9. }
  10. type Fields struct {
  11. Status Status `json:"status"`
  12. }
  13. type JsonResponse struct {
  14. Fields Fields `json:"fields"`
  15. }
  16. res := JsonResponse{}
  17. json.Unmarshal([]byte(jsonDataSrc), &res)
  18. fmt.Println(res.Fields.Status.Id)
  19. 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:

确定