GoでネストされたJSONを消化する際に問題が発生します。私の主な問題は、構造体を正しくモデル化してライブラリに情報を取得できないことです。JSONデータのサンプルは次のとおりです。http://pastebin.com/fcGQqi5z
データは銀行からのものであり、プライバシー保護のためにスクラブされています。理想的には、トランザクションID、金額、および説明のみに関心があります。Goでこれらの値を取得する方法はありますか?
これは私の最初の試みでした:
type Trans struct {
TransId string
Amount int
Description string
}
あなたは正しい軌道に乗っていました:
type Trans struct {
TransId string
Amount float64
Description string
}
func main() {
var data struct {
Record []Trans
}
if err := json.Unmarshal([]byte(j), &data); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%#v\n", data.Record)
}
//編集
type Trans struct {
TransId string
Amount float64
Description string
RawInfo []map[string]json.RawMessage `json:"AdditionalInfo"`
}
// also this assumes that 1. all data are strings and 2. they have unique keys
// if this isn't the case, you can use map[string][]string or something
func (t *Trans) AdditionalInfo() (m map[string]string) {
m = make(map[string]string, len(t.RawInfo))
for _, info := range t.RawInfo {
for k, v := range info {
m[k] = string(v)
}
}
return
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加