```
package main
import "fmt"
import "encoding/json"
func main() {
json_list_str := `[{"ID":"7fab2946-9d9c-39d0-387e-a0239cf95351","Node":"consul-4","Address":"192.168.15.113","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.15.113","wan":"192.168.15.113"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"web4","ServiceName":"web4","ServiceTags":["master"],"ServiceAddress":"192.168.15.113","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":8082,"ServiceEnableTagOverride":false,"ServiceProxyDestination":"","ServiceProxy":{},"ServiceConnect":{},"CreateIndex":2974,"ModifyIndex":2974}]`
var result []interface{}
err := json.Unmarshal([]byte(json_list_str), &result)
fmt.Println(result)
//todo 如何将“Address”取出?
}
```
非常感谢支持!
最终这样解决问题了。
```
package main
import "fmt"
import "encoding/json"
func main() {
json_list_str := `[{"ID":"7fab2946-9d9c-39d0-387e-a0239cf95351","Node":"consul-4","Address":"192.168.15.113","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.15.113","wan":"192.168.15.113"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"web4","ServiceName":"web4","ServiceTags":["master"],"ServiceAddress":"192.168.15.113","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":8082,"ServiceEnableTagOverride":false,"ServiceProxyDestination":"","ServiceProxy":{},"ServiceConnect":{},"CreateIndex":2974,"ModifyIndex":2974}]`
var result []map[string]interface{}
err := json.Unmarshal([]byte(json_list_str), &result)
fmt.Println(err, result)
// 取出result变量里面的"Address"字段。
fmt.Println(err, result[0]["Address"])
// output
// <nil> [map[ServiceKind: ServiceTags:[master] ServicePort:8082 ServiceProxyDestination: TaggedAddresses:map[lan:192.168.15.113 wan:192.168.15.113] ServiceAddress:192.168.15.113 ServiceEnableTagOverride:false ServiceProxy:map[] ServiceConnect:map[] ModifyIndex:2974 ID:7fab2946-9d9c-39d0-387e-a0239cf95351 Datacenter:dc1 ServiceID:web4 ServiceName:web4 ServiceMeta:map[] CreateIndex:2974 Node:consul-4 Address:192.168.15.113 NodeMeta:map[consul-network-segment:] ServiceWeights:map[Passing:1 Warning:1]]]
// <nil> 192.168.15.113
}
```
#4
更多评论