解析 json 的一个问题

jthmath · · 3611 次点击
解决了。应该搞成 map,而不是嵌套的 struct
#1
更多评论
解决了。应该搞成 map,而不是嵌套的 struct
#2
package main import ( "encoding/json" "fmt" ) type MqttClientConfigEntry struct { Broker string `json: "broker"` ClientId string `json: "clientId"` CleanSession bool `json: "cleanSession"` KeepAliveInterval uint32 `json: "keepAliveInterval"` Password string `json: "password"` UserName string `json: "userName"` PubTopic string `json: "pubTopic"` SubTopic string `json: "subTopic"` } type MqttClientConfig struct { MqttClient MqttClientConfigEntry `json: "MqttClient"` } const s = `{ "MqttClient": { "broker": "8888888888888888888/", "clientId": "11111111111111111", "cleanSession": true, "keepAliveInterval": 100, "password": "2222222222222", "userName": "3333333333333333", "pubTopic": "9999999999", "subTopic": "111111" } }` func main() { var mcc MqttClientConfig err := json.Unmarshal([]byte(s), &mcc) if err != nil { fmt.Println(err) } else { fmt.Println(mcc) } fmt.Println("#############data####") data, err := json.Marshal(&mcc) fmt.Println(err) fmt.Println(string(data)) }
#3