Home > Backend Development > Golang > How to Deserialize JSON with Dynamic Names in Nested Structures in Go?

How to Deserialize JSON with Dynamic Names in Nested Structures in Go?

Susan Sarandon
Release: 2024-11-22 21:24:15
Original
994 people have browsed it

How to Deserialize JSON with Dynamic Names in Nested Structures in Go?

Decoding Nested Dynamic Structures in JSON with Go

This article addresses the challenge of deserializing JSON data that features dynamic names within nested structures. Let's examine the problem and provide a solution.

Problem Statement

Consider the following JSON response:

{
    "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

struct:

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

The issue arises due to the dynamic phone numbers as property names within the "sms" object.

Solution

To handle this situation, we can utilize a map to represent the "sms" object in JSON:

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

Now, the deserialization code looks like this:

var result SMSSendJSON

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

This approach allows us to correctly process nested dynamic structures in Go.

The above is the detailed content of How to Deserialize JSON with Dynamic Names in Nested Structures in Go?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template