Home > Backend Development > Golang > Unmarshal dynamic json

Unmarshal dynamic json

王林
Release: 2024-02-08 22:36:32
forward
1087 people have browsed it

解组动态 json

php Xiaobian Zimo Unmarshaling dynamic JSON is a common programming technique used to convert JSON data into operable objects or arrays. In web development, JSON is a commonly used data exchange format, often used for front-end and back-end data transmission. Unmarshalling dynamic JSON makes it easy to extract and process the data within it, allowing developers to more flexibly manipulate and utilize JSON data. This article will introduce the basic concepts and usage of unmarshaling dynamic JSON to help readers better understand and apply this technique.

Question content

I have a bunch of json files that need to be unmarshalled. Their formats are basically the same, but the "length" is different

one example https://pastebin.com/htt6k658

another example https://pastebin.com/nr1z08f4

I tried several approaches, such as building a similar structure

type TagType struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
    Slug string `json:"slug"`
    tags []Tag  `json:"tags"`
}

type Tag struct {
    ID   int    `json:"users"`
    Name string `json:"name"`
    Slug string `json:"slug"`
}
Copy after login

There is also an interface, such as json.unmarshal([]byte(empjson), &result)

But none of these methods work.

Solution

You can use online tools, such as https://www.php.cn/link/fb25b181bed28630afa6c026a6ed31fe< /a> to generate go structure:

type AutoGenerated []struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
    Slug string `json:"slug"`
    Tags []struct {
        ID   int    `json:"id"`
        Name string `json:"name"`
        Slug string `json:"slug"`
        } `json:"tags"`
    }
Copy after login

The above is the detailed content of Unmarshal dynamic json. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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