go jsonとstructの相互マッピングの注意問題
6561 ワード
1、タグがない場合は、単語のアルファベットが同じ(大文字と小文字を区別しない)だけで、マッピングに成功する
しゅつりょく
2、structにstructを埋め込むと、内蔵structは値なしで入ってくるのも初期化されるので、値が入ってくるかどうかを判断するのはよくありません.structポインタを内蔵して、nilを判断することができます.
しゅつりょく
3、omitemptyは基本タイプのみに作用し、構造体には作用せず、ポインタを改称することができる
ポインタに変更
4、賦課問題
正しい値の割り当て
type MyRequest struct {
Id string `json:"im"`
Device MyDevice
Secure int
Status_Code int //
}
type MyDevice struct {
Ip string
Type int
}
func UnMarsh() {
s := `{"im": "123", "device":{"ip":"127.0.0.1"}, "sEcUrE":3, "status_code": 200}`
// s := `{"im": "123", "sEcUrE":3}`
var req MyRequest
err := json.Unmarshal([]byte(s), &req)
if err != nil {
panic(err)
}
fmt.Println(req)
}
しゅつりょく
{123 {127.0.0.1 0} 3 200}
//{123 { 0} 3 0}
2、structにstructを埋め込むと、内蔵structは値なしで入ってくるのも初期化されるので、値が入ってくるかどうかを判断するのはよくありません.structポインタを内蔵して、nilを判断することができます.
type MyRequest struct {
Id string `json:"im"`
Device *MyDevice
Secure int
}
type MyDevice struct {
Ip string
Type int
}
func UnMarsh () {
s := `{"im": "123", "device":{"ip":"127.0.0.1"}, "sEcUrE":3}`
// s := `{"im": "123", "sEcUrE":3}`
var req MyRequest
err := json.Unmarshal([]byte(s), &req)
if err != nil {
panic(err)
}
fmt.Println(req)
}
しゅつりょく
{123 0xc0000585a0 3}
// {123 3}
3、omitemptyは基本タイプのみに作用し、構造体には作用せず、ポインタを改称することができる
package jm
import (
"encoding/json"
"fmt"
"log"
)
type MyRequest struct {
Id string `json:"id,omitempty"`
Device []*MyDevice `json:"device,omitempty"`
Secure int `json:"secure,omitempty"`
Ext *MyExt `json:"ext,omitempty"`
}
type MyExt struct {
City string
}
type MyDevice struct {
Ip string `json:"ip,omitempty"`
Type int `json:"type,omitempty"`
App MyApp `json:"app,omitempty"` //
Model []string `json:"model,omitempty"`
}
type MyApp struct {
Name string
}
func Marsh() {
var req MyRequest
req.Id = "123"
var devs []*MyDevice
var device MyDevice
device.Ip = "0.0.0.0"
devs = append(devs, &device)
req.Device = devs
if bs, err := json.Marshal(req); err != nil {
log.Fatal(err)
} else {
fmt.Println(string(bs)) // {"id":"123","device":[{"ip":"0.0.0.0","app":{"Name":""}}]}
}
}
ポインタに変更
package jm
import (
"encoding/json"
"fmt"
"log"
)
type MyRequest struct {
Id string `json:"id,omitempty"`
Device []*MyDevice `json:"device,omitempty"`
Secure int `json:"secure,omitempty"`
Ext *MyExt `json:"ext,omitempty"`
}
type MyExt struct {
City string
}
type MyDevice struct {
Ip string `json:"ip,omitempty"`
Type int `json:"type,omitempty"`
App *MyApp `json:"app,omitempty"`
Model []string `json:"model,omitempty"`
}
type MyApp struct {
Name string
}
func Marsh() {
var req MyRequest
req.Id = "123"
var devs []*MyDevice
var device MyDevice
device.Ip = "0.0.0.0"
devs = append(devs, &device)
req.Device = devs
if bs, err := json.Marshal(req); err != nil {
log.Fatal(err)
} else {
fmt.Println(string(bs)) // {"id":"123","device":[{"ip":"0.0.0.0"}]}
}
}
4、賦課問題
package jm
import (
"encoding/json"
"fmt"
"log"
)
type MyRequest struct {
Id string `json:"id,omitempty"`
Device []*MyDevice `json:"device,omitempty"`
Secure int `json:"secure,omitempty"`
Ext *MyExt `json:"ext,omitempty"`
}
type MyExt struct {
City string
}
type MyDevice struct {
Ip string `json:"ip,omitempty"`
Type int `json:"type,omitempty"`
App *MyApp `json:"app,omitempty"`
}
type MyApp struct {
Name string
Hobby []*MyHobby
}
type MyHobby struct {
H string
}
func Marsh() {
var req MyRequest
req.Id = "123"
var devs []*MyDevice
var device MyDevice
device.Ip = "0.0.0.0"
device.App.Name = "T" //panic: runtime error: invalid memory address or nil pointer dereference
//var app MyApp
//app.Name = "an"
//var hob MyHobby
//hob.H = "sing"
//app.Hobby = append(app.Hobby, &hob)
//device.App = &app
//devs = append(devs, &device)
req.Device = devs
if bs, err := json.Marshal(req); err != nil {
log.Fatal(err)
} else {
fmt.Println(string(bs))
}
}
正しい値の割り当て
package jm
import (
"encoding/json"
"fmt"
"log"
)
type MyRequest struct {
Id string `json:"id,omitempty"`
Device []*MyDevice `json:"device,omitempty"`
Secure int `json:"secure,omitempty"`
Ext *MyExt `json:"ext,omitempty"`
}
type MyExt struct {
City string
}
type MyDevice struct {
Ip string `json:"ip,omitempty"`
Type int `json:"type,omitempty"`
App *MyApp `json:"app,omitempty"`
}
type MyApp struct {
Name string
Hobby []*MyHobby
}
type MyHobby struct {
H string
}
func Marsh() {
var req MyRequest
req.Id = "123"
var devs []*MyDevice
var device MyDevice
device.Ip = "0.0.0.0"
//device.App.Name = "T" //panic: runtime error: invalid memory address or nil pointer dereference
var app MyApp
app.Name = "an"
var hob MyHobby
hob.H = "sing"
app.Hobby = append(app.Hobby, &hob) // slice append
device.App = &app
device.Type = 0 // omitempty, ,json struct
devs = append(devs, &device)
req.Device = devs
if bs, err := json.Marshal(req); err != nil {
log.Fatal(err)
} else {
fmt.Println(string(bs)) // {"id":"123","device":[{"ip":"0.0.0.0","app":{"Name":"an","Hobby":[{"H":"sing"}]}}]}
}
}