如何在Golang中使用Unmarshal从json文档中获取结构中的json字符串

The sample code

package main

import (
    "encoding/json"
    "fmt"
)

type ClassRoom struct {
    Student struct {
        Name string
        /*
           Address struct {
               City string
               Zip  int
           } `json:"address"`
        */
        Address []string `json:"address"` //here, Want to get json string
        Age     int      `json:"age"`
    } `json:"student"`
    ClassCode int `json:"code"`
}

func main() {
    jsonDocs := `[
    {"student":{"name":"jss","address":{"City":"Seoul","Zip":54},"Age":28},"ClassCode":1234}]`

    var node []ClassRoom

    json.Unmarshal([]byte(jsonDocs), &node)
    fmt.Println(node)
}

I want Address variable of json string type (like {"City":"Seoul"....}).

You need to use json.RawMessage instead of []string, example:

type ClassRoom struct {
    Student struct {
        Name string
        Address json.RawMessage `json:"address"` //here, Want to get json string
        Age     int             `json:"age"`
    } `json:"student"`
    ClassCode int `json:"code"`
}

func main() {
    jsonDocs := `[{"student":{"name":"jss","address":{"City":"Seoul","Zip":54},"Age":28},"ClassCode":1234}]`
    var node []ClassRoom

    json.Unmarshal([]byte(jsonDocs), &node)
    fmt.Printf("%s
", node[0].Student.Address)

    var addr struct {
        City string
        Zip  int
    }
    json.Unmarshal(node[0].Student.Address, &addr)
    fmt.Printf("%+v", addr)
}

playground