简体   繁体   English

JSON嵌套动态结构进行解码

[英]JSON Nested dynamic structures Go decoding

There is an example on the input data. 在输入数据上有一个示例。

{
    "status": "OK",
    "status_code": 100,
    "sms": {
        "79607891234": {
            "status": "ERROR",
            "status_code": 203,
            "status_text": "Нет текста сообщения"
        },
        "79035671233": {
            "status": "ERROR",
            "status_code": 203,
            "status_text": "Нет текста сообщения"
        },
        "79105432212": {
            "status": "ERROR",
            "status_code": 203,
            "status_text": "Нет текста сообщения"
        }
    },
    "balance": 2676.18
}
type SMSPhone struct {
    Status     string `json:"status"`
    StatusCode int    `json:"status_code"`
    SmsID      string `json:"sms_id"`
    StatusText string `json:"status_text"`
}
type SMSSendJSON struct {
    Status     string     `json:"status"`
    StatusCode int        `json:"status_code"`
    Sms        []SMSPhone `json:"sms"`
    Balance    float64    `json:"balance"`
}

This is an example of the data that I receive after the appropriate request to the server. 这是在向服务器发出适当请求后我收到的数据的示例。 And I get such data. 我得到了这样的数据。 How can such data be serialized? 此类数据如何序列化? My attempt failed because of the dynamic names of the list of nested structures. 由于嵌套结构列表的动态名称,我的尝试失败了。 How can such nested dynamic structures be correctly processed? 如何正确处理此类嵌套的动态结构?

Use a map (of type map[string]SMSPhone ) to model the sms object in JSON: 使用地图(类型为map[string]SMSPhone )在JSON中对sms对象建模:

type SMSPhone struct {
    Status     string `json:"status"`
    StatusCode int    `json:"status_code"`
    StatusText string `json:"status_text"`
}

type SMSSendJSON struct {
    Status     string              `json:"status"`
    StatusCode int                 `json:"status_code"`
    Sms        map[string]SMSPhone `json:"sms"`
    Balance    float64             `json:"balance"`
}

Then unmarshaling: 然后拆封:

var result SMSSendJSON

if err := json.Unmarshal([]byte(src), &result); err != nil {
    panic(err)
}
fmt.Printf("%+v", result)

Will result in (try it on the Go Playground ): 将导致(在Go Playground上尝试):

{Status:OK StatusCode:100 Sms:map[79035671233:{Status:ERROR StatusCode:203 StatusText:Нет текста сообщения} 79105432212:{Status:ERROR StatusCode:203 StatusText:Нет текста сообщения} 79607891234:{Status:ERROR StatusCode:203 StatusText:Нет текста сообщения}] Balance:2676.18} {Status:OK StatusCode:100 Sms:map [79035671233:{Status:ERROR StatusCode:203 StatusText:Неттекстасообщения}} 79105432212:{Status:ERROR StatusCode:203 StatusText:Неттекстасообщения} 79607891234:{Status: StatusText:Неттекстасообщения}]余额:2676.18}

The keys in the result.Sms map are the "dynamic" properties of the object, namely the phone numbers. result.Sms映射中的键是对象的“动态”属性,即电话号码。

See related questions: 查看相关问题:

How to parse/deserlize a dynamic JSON in Golang 如何在Golang中解析/反序列化动态JSON

How to unmarshal JSON with unknown fieldnames to struct in golang? 如何解组未知字段名称的JSON以在Golang中进行结构化?

Unmarshal JSON with unknown fields 使用未知字段解组JSON

Unmarshal json string to a struct that have one element of the struct itself 将json字符串解组到具有结构本身一个元素的结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM