json - 在 Go 中解组 json
问题描述
我有以下 json 可以使用:
jsonStr := `{"Ok":[true,{"amount_awaiting_confirmation":"0","amount_awaiting_finalization":"0","amount_currently_spendable":"0","amount_immature":"0","amount_locked":"0","last_confirmed_height":"551632","minimum_confirmations":"10","total":"0"}]}`
这是我现在的处理方式:
resMap := make(map[string]interface{}, 0)
json.Unmarshal([]byte(jsonStr), &resMap)
if val, ok := resMap["Ok"]; ok {
tup := val.([]interface{})
wMap := tup[1].(map[string]interface{})
amountAwaitingConfirmation, _ := strconv.ParseInt(wMap["amount_awaiting_confirmation"].(string), 10, 64)
amountAwaitingFinalization, _ := strconv.ParseInt(wMap["amount_awaiting_finalization"].(string), 10, 64)
amountCurrentSpendable, _ := strconv.ParseInt(wMap["amount_currently_spendable"].(string), 10, 64)
amountImmature, _ := strconv.ParseInt(wMap["amount_immature"].(string), 10, 64)
amountLocked, _ := strconv.ParseInt(wMap["amount_locked"].(string), 10, 64)
lastConfirmedHeight, _ := strconv.ParseInt(wMap["last_confirmed_height"].(string), 10, 64)
minimumConfirmations, _ := strconv.ParseInt(wMap["minimum_confirmations"].(string), 10, 64)
total, _ := strconv.ParseInt(wMap["total"].(string), 10, 64)
}
有没有一种更简单的方法来处理这个结构,而不必求助于通用 interface{} 转换?
解决方案
为了避免所有手动类型断言,您可以做的一件事是使用json.RawMessage
. 然后,您至少可以使用结构来解组列表的对象部分。
这是一个使用您发布的 JSON 字符串的示例(这里它在Go Playground中运行):
type OkJson struct {
Ok []json.RawMessage
}
type Details struct {
AmountAwaitingConfirmation string `json:"amount_awaiting_confirmation"`
AmountAwaitingFinalization string `json:"amount_awaiting_finalization"`
AmountCurrentlySpendable string `json:"amount_currently_spendable"`
AmountImmature string `json:"amount_immature"`
AmountLocked string `json:"amount_locked"`
LastConfirmedHeight string `json:"last_confirmed_height"`
MinimumConfirmations string `json:"minimum_confirmations"`
Total string `json:"total"`
}
func main() {
var okj OkJson
_ = json.Unmarshal([]byte(jsonStr), &okj)
var aBool bool
_ = json.Unmarshal(okj.Ok[0], &aBool)
fmt.Println(aBool)
var details Details
_ = json.Unmarshal(okj.Ok[1], &details)
fmt.Println(details)
}
推荐阅读
- machine-learning - 我在训练 BERT 模型时出错
- java - 如何在android中模糊imageview的背景图像
- html - 选择不带标签的自由子元素的 XPath
- typo3 - TYPO3/TypoScript - 在 DataProcessor 中使用 FIELD() 自定义排序
- node.js - NodeJS中axios和supertest的区别
- c++ - 我可以禁止临时对象作为参数吗?
- angular - Angular 9:过滤器。为什么从服务器获取数据时过滤器在角度材料表中不起作用?
- c# - 模拟.Setup(...).Returns(..); 不嘲讽
- android - 如何从存储库中获取 sharedPreferences
- python - subprocess.run 输出为空(python 3.8)