在Go语言中获取内部JSON值

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

Get Inner JSON Value in Go

问题

我可以帮你翻译代码部分。以下是翻译的结果:

简单的问题,我在如何为JSON解码结构体的结构上遇到了困难。

如何将一个结构体的内部字段复制到另一个结构体的字段中?

我有以下JSON数据:

  1. {
  2. "Trains": [{
  3. "Car": "6",
  4. "Destination": "SilvrSpg",
  5. "DestinationCode": "B08",
  6. "DestinationName": "Silver Spring",
  7. "Group": "1",
  8. "Line": "RD",
  9. "LocationCode": "A13",
  10. "LocationName": "Twinbrook",
  11. "Min": "1"
  12. }]
  13. }

我有以下结构体:

  1. type Trains struct {
  2. Min string `json:"Min"`
  3. DestName string `json:"DestinationName"`
  4. DestCode string `json:"DestinationCode"`
  5. LocName string `json:"LocationName"`
  6. LocCode string `json:"LocationCode"`
  7. Line string `json:"Line"`
  8. }
  9. type AllData struct {
  10. Data []Trains `json:"Trains"`
  11. }

我如何将Trains.LocationCode的值复制到类似以下结构体的位置:

  1. type AllData struct {
  2. Id string `json:"Id"`
  3. Data []Trains `json:"Trains"`
  4. }

所以,我基本上只需要有以下JSON结构:

  1. {
  2. "Id": "A13",
  3. "Data": [{
  4. "Car": "6",
  5. "Destination": "SilvrSpg",
  6. "DestinationCode": "B08",
  7. "DestinationName": "Silver Spring",
  8. "Group": "1",
  9. "Line": "RD",
  10. "LocationCode": "A13",
  11. "LocationName": "Twinbrook",
  12. "Min": "1"
  13. }]
  14. }

其中,Id是Trains结构体的内部值。

你想要如何构造一个结构体来反映这个结构?

英文:

Simple question that I'm having a difficult time how to structure a struct for JSON decoding.

How can I copy an inner field of a struct to another field of a struct?

I have JSON

  1. {
  2. "Trains": [{
  3. "Car": "6",
  4. "Destination": "SilvrSpg",
  5. "DestinationCode": "B08",
  6. "DestinationName": "Silver Spring",
  7. "Group": "1",
  8. "Line": "RD",
  9. "LocationCode": "A13",
  10. "LocationName": "Twinbrook",
  11. "Min": "1"
  12. }]
  13. }

And I have structs

  1. type Trains struct {
  2. Min string `json:"Min"`
  3. DestName string `json:"DestinationName"`
  4. DestCode string `json:"DestinationCode"`
  5. LocName string `json:"LocationName"`
  6. LocCode string `json:"LocationCode"`
  7. Line string `json:"Line"`
  8. }
  9. type AllData struct {
  10. Data []Trains `json:"Trains"`
  11. }

How Can I get the value of the Trains.LocationCode to a struct like

  1. type AllData struct {
  2. Id Trains[0].LocCode value
  3. Data []Trains `json:"Trains"`
  4. }

So I basically just need to have JSON like this

  1. {
  2. "Id":"A13",
  3. "Data": [{
  4. "Car": "6",
  5. "Destination": "SilvrSpg",
  6. "DestinationCode": "B08",
  7. "DestinationName": "Silver Spring",
  8. "Group": "1",
  9. "Line": "RD",
  10. "LocationCode": "A13",
  11. "LocationName": "Twinbrook",
  12. "Min": "1"
  13. }]
  14. }

Where the Id is the inner value of the Trains struct.

How can I structure a struct to reflect this?

答案1

得分: 0

JSON解码器没有这个功能。您必须在您的应用程序中编写这行代码。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. var s = `
  8. {
  9. "Trains": [{
  10. "Car": "6",
  11. "Destination": "SilvrSpg",
  12. "DestinationCode": "B08",
  13. "DestinationName": "Silver Spring",
  14. "Group": "1",
  15. "Line": "RD",
  16. "LocationCode": "A13",
  17. "LocationName": "Twinbrook",
  18. "Min": "1"
  19. }]
  20. }`
  21. type Train struct {
  22. Min string `json:"Min"`
  23. DestName string `json:"DestinationName"`
  24. DestCode string `json:"DestinationCode"`
  25. LocName string `json:"LocationName"`
  26. LocCode string `json:"LocationCode"`
  27. Line string `json:"Line"`
  28. }
  29. type Data struct {
  30. // The name "-" tells the JSON decoder to ignore this field.
  31. ID string `json:"-"`
  32. Trains []Train
  33. }
  34. func main() {
  35. var d Data
  36. if err := json.Unmarshal([]byte(s), &d); err != nil {
  37. log.Fatal(err)
  38. }
  39. if len(d.Trains) < 1 {
  40. log.Fatal("No trains")
  41. }
  42. // Copy value from inner to outer.
  43. d.ID = d.Trains[0].LocCode
  44. fmt.Printf("%+v\n", d)
  45. }
英文:

The JSON decoder does not have this capability. You must write the line of code in your application.

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. )
  7. var s = `
  8. {
  9. &quot;Trains&quot;: [{
  10. &quot;Car&quot;: &quot;6&quot;,
  11. &quot;Destination&quot;: &quot;SilvrSpg&quot;,
  12. &quot;DestinationCode&quot;: &quot;B08&quot;,
  13. &quot;DestinationName&quot;: &quot;Silver Spring&quot;,
  14. &quot;Group&quot;: &quot;1&quot;,
  15. &quot;Line&quot;: &quot;RD&quot;,
  16. &quot;LocationCode&quot;: &quot;A13&quot;,
  17. &quot;LocationName&quot;: &quot;Twinbrook&quot;,
  18. &quot;Min&quot;: &quot;1&quot;
  19. }]
  20. }`
  21. type Train struct {
  22. Min string `json:&quot;Min&quot;`
  23. DestName string `json:&quot;DestinationName&quot;`
  24. DestCode string `json:&quot;DestinationCode&quot;`
  25. LocName string `json:&quot;LocationName&quot;`
  26. LocCode string `json:&quot;LocationCode&quot;`
  27. Line string `json:&quot;Line&quot;`
  28. }
  29. type Data struct {
  30. // The name &quot;-&quot; tells the JSON decoder to ignore this field.
  31. ID string `json:&quot;-&quot;`
  32. Trains []Train
  33. }
  34. func main() {
  35. var d Data
  36. if err := json.Unmarshal([]byte(s), &amp;d); err != nil {
  37. log.Fatal(err)
  38. }
  39. if len(d.Trains) &lt; 1 {
  40. log.Fatal(&quot;No trains&quot;)
  41. }
  42. // Copy value from inner to outer.
  43. d.ID = d.Trains[0].LocCode
  44. fmt.Printf(&quot;%+v\n&quot;, &amp;d)
  45. }

huangapple
  • 本文由 发表于 2014年9月17日 23:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/25894352.html
匿名

发表评论

匿名网友

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

确定