登录
首页 >  Golang >  Go问答

在go语言中使用struct解析嵌套的json数据

来源:stackoverflow

时间:2024-02-06 10:48:23 154浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《在go语言中使用struct解析嵌套的json数据》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

无法使用 go lang 将嵌套 json 解析为结构对象

我有一个嵌套的 json 字符串,我想使用 go 语言中的结构体来解析它。 json 看起来像这样

{"action":"add","business":{"listid":123,"objecttags":[{"tagcode":"csharp","tagname":"codename","tagvalue":["2"],"tagtype":3},{"tagcode":"golang","tagname":"coding","tagvalue":["3"],"tagtype":3}]}}

我想用go语言解析json。 json 具有嵌套结构,因此我创建了以下代码中提到的结构

package main

import (
    "encoding/json"
    "fmt"
)


type objecttagslist struct {
    tagcode  string
    tagname  string
    tagvalue []string
}

type model struct {
    action   string `json:"action"`
    business struct {
        listid     int64  `json:"listid"`
        objecttags []objecttagslist `json:"objecttags"`
    } `json:"business"`
}

func main() {
    json := `{"action":"add","business":{"listid":123,"objecttags":[{"tagcode":"csharp","tagname":"codename","tagvalue":["2"],"tagtype":3},{"tagcode":"golang","tagname":"coding","tagvalue":["3"],"tagtype":3}]}}`

    var model model
    json.unmarshal([]byte(json), &model)

    fmt.println(model.action) // this prints correctly as "add"
        fmt.println(model.business.listid) // this prints correctly as "123"


    fmt.println(model.business.objecttags) // this does not print the objecttags. rather this prints the objecttags as "[{  []} {  []}]"


}

我无法将内部嵌套 json 的值获取到结构中。

我还尝试再次解组内部结构

var object []objecttagslist

//this gives error as cannot convert model.business.objecttags (variable of type []objecttagslist) to type []byte

json.unmarshal([]byte(model.business.objecttags), &object)

//错误,无法将 model.business.objecttags([]objecttagslist 类型的变量)转换为 []byte 类型

fmt.println(object)

这给了我一个错误 无法将 model.business.objecttags([]objecttagslist 类型的变量)转换为 []byte 类型。

如何将此 json 映射到结构中? 我想以这样的方式映射它,以便我可以使用像

这样的对象
model.Business.ObjectTags[0].tagCode //--> Should print/store "csharp"
model.Business.ObjectTags[0].tagValue[0] //--> Should print/store "2"

请帮忙


正确答案


您只能编组/取消编组“导出”字段——即可以在当前包外部访问的字段,这在 go 中意味着“以大写字母开头的字段”。因此,如果您要将代码修改为如下所示:

package main

import (
    "encoding/json"
    "fmt"
)

type objecttagslist struct {
    tagcode  string
    tagname  string
    tagvalue []string
}

type model struct {
    action   string `json:"action"`
    business struct {
        listid     int64            `json:"listid"`
        objecttags []objecttagslist `json:"objecttags"`
    } `json:"business"`
}

func main() {
    json := `
{
  "action": "add",
  "business": {
    "listid": 123,
    "objecttags": [
      {
        "tagcode": "csharp",
        "tagname": "codename",
        "tagvalue": [
          "2"
        ],
        "tagtype": 3
      },
      {
        "tagcode": "golang",
        "tagname": "coding",
        "tagvalue": [
          "3"
        ],
        "tagtype": 3
      }
    ]
  }
}
`

    var model model
    json.unmarshal([]byte(json), &model)

    fmt.println(model.action)
    fmt.println(model.business.listid)

    fmt.println(model.business.objecttags)
}

您将得到输出:

add
123
[{csharp codename [2]} {golang coding [3]}]

这里我们利用了 json 模块会自动将名为 tagcode 的键映射到名为 tagcode 的结构体字段的事实,但实际上我们应该明确:

type ObjectTagsList struct {
    TagCode  string   `json:"tagCode"`
    TagName  string   `json:"tagName"`
    TagValue []string `json:"tagValue"`
}

本篇关于《在go语言中使用struct解析嵌套的json数据》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

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