示例代码
<div id="linksbwrap">
<a class="linkb" href="">Lorem ipsum Lorem ipsum</a><br>
<a class="linkb" href="">Lorem ipsum Lorem</a><br>
<a class="linkb" href="">Lorem ipsum Lorem ipsum</a><br>
<a class="linkb" href="">Lorem ipsum Lorem</a><br>
<a class="linkb" href="">Lorem ipsum Lorem ipsum</a><br>
<a class="linkb" href="">Lorem ipsum Lorem</a><br>
<a class="linkb" href="">Lorem ipsum Lorem ipsum</a><br>
</div>
我想要json字符串类型的Address变量(如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)
}
)。
答案 0 :(得分:0)
您需要使用json.RawMessage
代替[]string
,例如:
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\n", node[0].Student.Address)
var addr struct {
City string
Zip int
}
json.Unmarshal(node[0].Student.Address, &addr)
fmt.Printf("%+v", addr)
}