..
一个解析json的例子
Golang 实现了数据的请求,下面是对返回的json格式的数据进行解析的例子。
package main
import (
"encoding/json"
"fmt"
)
type TokenRole struct {
TokenRoleGroup uint32
TokenRoleID string
}
type TokenRoles struct {
TokenRole TokenRole
}
/// TokenContext
type TokenContext struct {
Flags uint32
LedgerEntryType string
LowNode string
TokenRoles []TokenRoles
Index string `json:"index"`
}
type TokenContextResult struct {
LedgerCurrentIndex uint32 `json:"ledger_current_index"`
TokenContext TokenContext
Status string `json:"status"`
Validated bool `json:"validated"`
}
type Result struct {
Result TokenContextResult `json:"result"`
}
func main() {
var err error
jsonStr := []byte(`
{
"result":{
"TokenContext":{
"Flags":0,
"LedgerEntryType":"TokenContext",
"LowNode":"0000000000000000",
"TokenRoles":[
{
"TokenRole":{
"TokenRoleGroup":1,
"TokenRoleID":"jswPdzRsZbNE3KRQBPYYxh7rbvJDjiMSGi"
}
},
{
"TokenRole":{
"TokenRoleGroup":2,
"TokenRoleID":"jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh"
}
}
],
"index":"D98F6C71E7391ECD67FC4008B7E946ABA617E52B27650A15DA465501B54F1E7F"
},
"ledger_current_index":4644,
"status":"success",
"validated":false
}
}
`)
var result Result
err = json.Unmarshal(jsonStr, &result)
if err != nil {
panic("解析失败")
}
fmt.Println("ledger current index: ", result.Result.LedgerCurrentIndex) //output: 4644
}
Nothing