英文:
How do I parse JSON in Go?
问题
JSON
{
"id" : "12387",
"inv" : [
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" : [
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
},
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" : [
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
}
],
"charges" : {
"fee" : 24 ,
"bkg" : 7676
}
}
我的JSON结构如上所示。我需要获取inv-addCharges
中的amnt
,并将其放入一个数组中。如果数组中有十个元素,我需要将其放入一个只包含特定amnt
的数组中,如下所示:
[{ "amnt" : 34 }, { "amnt" : 34 }, ....]
我尝试过的方法:
var j map[string]interface{}
err := json.Unmarshal([]byte(ticket), &j)
if err != nil {
panic(err)
}
// 提取inv对象
bytInv := j["inv"].([]interface{})
// 打印出inv对象
fmt.Println(bytInv)
输出结果:
[map[qty:5 seq:3 invIs:1HG9876 addCharges:[map[amnt:24 char:REI type:MT] map[amnt:24 char:REI type:MT]]] map[qty:5 seq:3 invIs:1HG9876 addCharges:[map[amnt:24 char:REI type:MT] map[amnt:24 char:REI type:MT]]]]
在这之后,我无法进一步进行。
注意: 我不想使用结构体,因为我有很多这样的JSON结构。这是我的要求。
英文:
JSON
{
"id" : "12387",
"inv" :[
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
},
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
}
],
"charges" : {
"fee" : 24 ,
"bkg" : 7676
}
}
My JSON structure is like that shown above. I need to take amnt in inv-addCharges
in an array. If it has ten elements in the array, I need to get that in an array containing the particular amnt
alone in such a way like
[{"amnt" : 34 } ,{"amnt" : 34} .... so on ]
Things I tried:
var j map[string]interface{}
err := json.Unmarshal([]byte(ticket), &j)
if err != nil {
panic(err)
}
// Pull out the parents object
bytInv := j["inv"].([]interface{})
// // Print out mother and father
fmt.Println(bytInv)
Output
[map[qty:5 seq:3 invIs:1HG9876 addCharges:[map[amnt:24 char:REI type:MT] map[amnt:24 char:REI type:MT]]] map[qty:5 seq:3 invIs:1HG9876 addCharges:[map[amnt:24 char:REI type:MT] map[amnt:24 char:REI type:MT]]]]
After this, I was not able to proceed any further.
Note: I don't want to use structs because I have many JSON structures for this. And that's my requirement given.
答案1
得分: 6
你是否要求我翻译以下内容?
"Requirement or not, I really don't see why you insist on not using structs for this.
Can you elaborate on exactly why this is the case?
As evidenced by the example program below, the solution is so much easier to understand and reason about if you just represent the data as properly typed structs. You can run it on the Go playground to see it in action.
There is one part of your JSON data which confuses me. Each Item node contains the seq
field twice. Both with a different value. Which ever way you use to interpret the data, one of the fields will be lost. Depending on the implementation of the unmarshaller, it will either ignore the second field, or overwrite the first one with the second value. This is a bug in the way your data is generated.
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
charges, err := findCharges()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
for _, c := range charges {
fmt.Printf("%+v\n", c)
}
}
// findCharges locates all 'AddCharge` instances and returns them in a slice.
func findCharges() ([]AddCharge, error) {
var prod Product
data := []byte(jsonString)
err := json.Unmarshal(data, &prod)
if err != nil {
return nil, err
}
var charges []AddCharge
for _, item := range prod.Items {
charges = append(charges, item.AddCharges...)
}
return charges, nil
}
type Product struct {
Id string `json:"id"`
Items []Item `json:"inv"`
}
type Item struct {
Quantity int `json:"qty"`
Sequence int `json:"seq"`
Inventory string `json:"invIs"`
AddCharges []AddCharge `json:"addCharges"`
Charges []Charge `json:"charges"`
}
type Charge struct {
Fee int `json:"fee"`
Bkg int `json:"bkg"`
}
type AddCharge struct {
Amount int `json:"amnt"`
Char string `json:"char"`
Type string `json:"type"`
}
const jsonString = `{
"id" : "12387",
"inv" :[
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 12,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
},
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 64,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 36,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
}
],
"charges" : {
"fee" : 24 ,
"bkg" : 7676
}
}"
编辑:如果你真的只对addCharges
数据感兴趣,你可以从结构体中省略所有不需要的字段。解组器将简单地忽略结构体没有定义字段的数据。
英文:
Requirement or not, I really don't see why you insist on not using structs for this.
Can you elaborate on exactly why this is the case?
As evidenced by the example program below, the solution is so much easier to understand and reason about if you just represent the data as properly typed structs. You can run it on the Go playground to see it in action.
There is one part of your JSON data which confuses me. Each Item node contains the seq
field twice. Both with a different value. Which ever way you use to interpret the data, one of the fields will be lost. Depending on the implementation of the unmarshaller, it will either ignore the second field, or overwrite the first one with the second value. This is a bug in the way your data is generated.
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
charges, err := findCharges()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
for _, c := range charges {
fmt.Printf("%+v\n", c)
}
}
// findCharges locates all 'AddCharge` instances and returns them in a slice.
func findCharges() ([]AddCharge, error) {
var prod Product
data := []byte(jsonString)
err := json.Unmarshal(data, &prod)
if err != nil {
return nil, err
}
var charges []AddCharge
for _, item := range prod.Items {
charges = append(charges, item.AddCharges...)
}
return charges, nil
}
type Product struct {
Id string `json:"id"`
Items []Item `json:"inv"`
}
type Item struct {
Quantity int `json:"qty"`
Sequence int `json:"seq"`
Inventory string `json:"invIs"`
AddCharges []AddCharge `json:"addCharges"`
Charges []Charge `json:"charges"`
}
type Charge struct {
Fee int `json:"fee"`
Bkg int `json:"bkg"`
}
type AddCharge struct {
Amount int `json:"amnt"`
Char string `json:"char"`
Type string `json:"type"`
}
const jsonString = `{
"id" : "12387",
"inv" :[
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 24,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 12,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
},
{
"qty" : 5,
"seq" : 2,
"invIs" : "1HG9876",
"addCharges" :[
{
"amnt" : 64,
"char" : "REI",
"type" : "MT"
},
{
"amnt" : 36,
"char" : "REI",
"type" : "MT"
}
],
"seq" : 3
}
],
"charges" : {
"fee" : 24 ,
"bkg" : 7676
}
}`
edit: If the addCharges
data is really all you are interested in, you can omit all the fields you do not need from the structs. The unmarshaller will simply ignore the data for which the struct has no field defined.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论