golang 复杂json Unmarshal后如何取值

weixin_38048154 2019-09-20 10:55:26
```
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”取出?
}
```
...全文
279 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38079044 2019-09-20
  • 打赏
  • 举报
回复
``` var result map[string]interface{} err := json.Unmarshal([]byte(json_list_str), &result) fmt.Println(err, result) ``` 提示报错了。 json: cannot unmarshal array into Go value of type map[string]interface {} map[] 在线代码执行链接 https://www.bytelang.com/o/s/c/c9YRSqkXPFs=
weixin_38087157 2019-09-20
  • 打赏
  • 举报
回复 1
非常感谢支持! 最终这样解决问题了。 ``` 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 } ```
weixin_38096743 2019-09-20
  • 打赏
  • 举报
回复
某些复杂的json也可以考虑这个包:https://github.com/tidwall/gjson
weixin_38052215 2019-09-20
  • 打赏
  • 举报
回复
map[string]interface{}
weixin_38060006 2019-09-20
  • 打赏
  • 举报
回复
json数据里面的"Address":"192.168.15.113",需要如何取出来?

435

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧