在Go语言中获取内部JSON值

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

Get Inner JSON Value in Go

问题

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

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

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

我有以下JSON数据:

{
    "Trains": [{
        "Car": "6",
        "Destination": "SilvrSpg",
        "DestinationCode": "B08",
        "DestinationName": "Silver Spring",
        "Group": "1",
        "Line": "RD",
        "LocationCode": "A13",
        "LocationName": "Twinbrook",
        "Min": "1"
    }]
}

我有以下结构体:

type Trains struct {
  Min      string `json:"Min"`
  DestName string `json:"DestinationName"`
  DestCode string `json:"DestinationCode"`
  LocName  string `json:"LocationName"`
  LocCode  string `json:"LocationCode"`
  Line     string `json:"Line"`
}

type AllData struct {
  Data []Trains `json:"Trains"`
}

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

type AllData struct {
  Id   string   `json:"Id"`
  Data []Trains `json:"Trains"`
}

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

{
    "Id": "A13",
    "Data": [{
        "Car": "6",
        "Destination": "SilvrSpg",
        "DestinationCode": "B08",
        "DestinationName": "Silver Spring",
        "Group": "1",
        "Line": "RD",
        "LocationCode": "A13",
        "LocationName": "Twinbrook",
        "Min": "1"
    }]
}

其中,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

{
    "Trains": [{
        "Car": "6",
        "Destination": "SilvrSpg",
        "DestinationCode": "B08",
        "DestinationName": "Silver Spring",
        "Group": "1",
        "Line": "RD",
        "LocationCode": "A13",
        "LocationName": "Twinbrook",
        "Min": "1"
    }]
}

And I have structs

type Trains struct {
  Min      string `json:"Min"`
  DestName string `json:"DestinationName"`
  DestCode string `json:"DestinationCode"`
  LocName  string `json:"LocationName"`
  LocCode  string `json:"LocationCode"`
  Line     string `json:"Line"`
}

type AllData struct {
  Data []Trains `json:"Trains"`
}

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

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

So I basically just need to have JSON like this

{
    "Id":"A13",
    "Data": [{
        "Car": "6",
        "Destination": "SilvrSpg",
        "DestinationCode": "B08",
        "DestinationName": "Silver Spring",
        "Group": "1",
        "Line": "RD",
        "LocationCode": "A13",
        "LocationName": "Twinbrook",
        "Min": "1"
    }]
}

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

How can I structure a struct to reflect this?

答案1

得分: 0

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

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

var s = `
{
    "Trains": [{
        "Car": "6",
        "Destination": "SilvrSpg",
        "DestinationCode": "B08",
        "DestinationName": "Silver Spring",
        "Group": "1",
        "Line": "RD",
        "LocationCode": "A13",
        "LocationName": "Twinbrook",
        "Min": "1"
    }]
}`

type Train struct {
    Min      string `json:"Min"`
    DestName string `json:"DestinationName"`
    DestCode string `json:"DestinationCode"`
    LocName  string `json:"LocationName"`
    LocCode  string `json:"LocationCode"`
    Line     string `json:"Line"`
}

type Data struct {
    // The name "-" tells the JSON decoder to ignore this field.
    ID     string `json:"-"`
    Trains []Train
}

func main() {
    var d Data
    if err := json.Unmarshal([]byte(s), &d); err != nil {
        log.Fatal(err)
    }
    if len(d.Trains) < 1 {
        log.Fatal("No trains")
    }
    // Copy value from inner to outer.
    d.ID = d.Trains[0].LocCode
    fmt.Printf("%+v\n", d)
}
英文:

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

package main

import (
    &quot;encoding/json&quot;
    &quot;fmt&quot;
    &quot;log&quot;
)

var s = `
{
    &quot;Trains&quot;: [{
        &quot;Car&quot;: &quot;6&quot;,
        &quot;Destination&quot;: &quot;SilvrSpg&quot;,
        &quot;DestinationCode&quot;: &quot;B08&quot;,
        &quot;DestinationName&quot;: &quot;Silver Spring&quot;,
        &quot;Group&quot;: &quot;1&quot;,
        &quot;Line&quot;: &quot;RD&quot;,
        &quot;LocationCode&quot;: &quot;A13&quot;,
        &quot;LocationName&quot;: &quot;Twinbrook&quot;,
        &quot;Min&quot;: &quot;1&quot;
    }]
}`

type Train struct {
    Min      string `json:&quot;Min&quot;`
    DestName string `json:&quot;DestinationName&quot;`
    DestCode string `json:&quot;DestinationCode&quot;`
    LocName  string `json:&quot;LocationName&quot;`
    LocCode  string `json:&quot;LocationCode&quot;`
    Line     string `json:&quot;Line&quot;`
}

type Data struct {
    // The name &quot;-&quot; tells the JSON decoder to ignore this field.
    ID     string `json:&quot;-&quot;`
    Trains []Train
}

func main() {
    var d Data
    if err := json.Unmarshal([]byte(s), &amp;d); err != nil {
        log.Fatal(err)
    }
    if len(d.Trains) &lt; 1 {
        log.Fatal(&quot;No trains&quot;)
    }
    // Copy value from inner to outer.
    d.ID = d.Trains[0].LocCode
    fmt.Printf(&quot;%+v\n&quot;, &amp;d)
}

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:

确定