Home  >  Article  >  Backend Development  >  Using struct to parse nested json in go language

Using struct to parse nested json in go language

WBOY
WBOYforward
2024-02-06 08:15:09358browse

Using struct to parse nested json in go language

Question content

Unable to use go lang to parse nested json into a structure object

I have a nested json string and I want to parse it using structures in the go language. json looks like this

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

I want to parse json using go language. json has nested structure so I created the structure mentioned in the following code

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 "[{  []} {  []}]"


}

I can't get the value of the inner nested json into the structure.

I also tried to unmarshal the internal structure again

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)

//Error, cannot convert model.business.objecttags (variable of type []objecttagslist) to type []byte

fmt.println(object)

This gives me an error Unable to convert model.business.objecttags (variable of type []objecttagslist) to type []byte.

How to map this json into a structure? I want to map it in such a way that I can use an object like

model.Business.ObjectTags[0].tagCode //--> Should print/store "csharp"
model.Business.ObjectTags[0].tagValue[0] //--> Should print/store "2"

Please help


Correct answer


You can only marshal/unmarshal "exported" fields - i.e. fields that can be accessed outside the current package, this is In go it means "fields starting with a capital letter". So if you were to modify your code to look like this:

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)
}

You will get the output:

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

Here we take advantage of the fact that the json module will automatically map the key named tagcode to the structure field named tagcode, but in fact We should be clear:

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

The above is the detailed content of Using struct to parse nested json in go language. 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