卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34082本站已运行391

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

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"`
}
卓越飞翔博客
上一篇: Gorm:如何在字段中存储结构
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏