尝试通过 Go 解析 JSON 对象并遍历记录并仅打印选定的字段。使用 fmt 打印时面临无法参考的问题。
JSON对象结构:
type myBalanceInfo struct {
Name string `json:"name"`
Id int64 `json:"id"`
Balances map[string]myBalance `json:"balances"`
}
type myBalance[]myBalanceInfo
type myBalance struct {
Available decimal.Decimal `json:"available"`
Balance decimal.Decimal `json:"balance"`
}
临时文件:
{
"abc": {
"name": "abc",
"id": 1,
"balances": {
"acc1": {
"available": "1.2",
"balance": "1.2"
},
"acc2": {
"available": "2.000001",
"balance": "2.000001"
},
"acc3": {
"available": "0.000001",
"balance": "0.000001"
},
"acc4": {
"available": "1000",
"balance": "1000"
}
}
}
}
主要的:
jsonInputFile := os.Open("temp.json")
defer jsonInputFile.Close()
jsonByteValue, _ := ioutil.ReadAll(jsonInputFile )
var data []myBalanceInfo
json.Unmarshal([]byte(jsonByteValue), &data )
for i := 0; i < (len(data)); i++ {
fmt.Println("ID: " + strconv.FormatInt(data[i].Id, 10))
fmt.Println("Balance: " + decimal.NewFromString(data[i].Balance))
}
错误:
无效操作:“余额:”+ walletData[i].Balance(不匹配的类型字符串和小数。十进制)”