Home > Backend Development > Golang > How to Decode JSON with Dynamic Nested Structures in Golang?

How to Decode JSON with Dynamic Nested Structures in Golang?

DDD
Release: 2024-11-17 08:46:03
Original
534 people have browsed it

How to Decode JSON with Dynamic Nested Structures in Golang?

JSON Nested Dynamic Structures Go Decoding

Encounters often arise when working with JSON data that contains dynamic or unknown field names within nested structures. This can pose a challenge when attempting to accurately deserialize and process the data.

Example Data

Consider the following JSON data received after a server request:

{
    "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
}
Copy after login

Custom Type Definitions

To handle this dynamic structure, custom type definitions can be employed:

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        []SMSPhone `json:"sms"`
    Balance    float64    `json:"balance"`
}
Copy after login

Using Maps

Since the sms object in the JSON has dynamic keys, it can be modeled using a map with the phone numbers as keys and the SMSPhone struct as the values:

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"`
}
Copy after login

Unmarshaling

With the custom types defined, the data can be unmarshaled as follows:

var result SMSSendJSON

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

This will result in a properly deserialized object with the dynamic phone numbers as keys in the result.Sms map.

Related Questions

  • How to parse/deserialize a dynamic JSON in Golang
  • How to unmarshal JSON with unknown fieldnames to struct in golang?
  • Unmarshal JSON with unknown fields
  • Unmarshal json string to a struct that has one element of the struct itself

The above is the detailed content of How to Decode JSON with Dynamic Nested Structures in Golang?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template