Home  >  Article  >  Backend Development  >  Unmarshal dynamic json

Unmarshal dynamic json

王林
王林forward
2024-02-08 22:36:321048browse

解组动态 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"`
}

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 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"`
    }

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

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete