英文:
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 (
"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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论