登录
首页 >  Golang >  Go问答

创建符合标准的 JSON 结构的方法

来源:stackoverflow

时间:2024-03-07 23:33:25 214浏览 收藏

从现在开始,努力学习吧!本文《创建符合标准的 JSON 结构的方法》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

如何正确解析json我有以下json文件

{
    "hello": {
        "title": "golang",
        "story": [
            "go lang story",
            "channel story"
        ],
        "options": [
            {
                "text": "that story",
                "arc": "west"
            },
            {
                "text": "gee",
                "arc": "east"
            }
        ]
    },
    "world": {
        "title": "visiting",
        "story": [
            "boo",
            "doo",
            "moo",
            "qoo"
        ],
        "options": [
            {
                "text": "weird",
                "arc": "west"
            },
            {
                "text": "funny",
                "arc": "north"
            }
        ]
    }
}

我已经为内部部分创建了这些结构

type chapter struct{
    Title string `json:"title"`
    Story []string `json:"story"`
    Option []option `json:"options"`
}

type option struct {
    Text string `json:"text"`
    Arc string `json:"arc"`
}

但我不知道如何解析“hello”和“world”等包装器


解决方案


构建根映射所需的一切。

{
    "hello":{},
    "world":{}
}

这里的 helloworld 也在地图内。所以你也需要构建它们。

var root map[string]chapter
 json.Unmarshal(JSONDATA,&root)

演示示例:https://play.golang.org/p/VZ9Bn215dDW

到这里,我们也就讲完了《创建符合标准的 JSON 结构的方法》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>