复杂的json数据怎么像python一样读取啊

jiangyd · · 926 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

``` { "PageNumber": 1, "TotalCount": 1, "PageSize": 10, "RequestId": "8DADF6F2-3C20-40A4-B48F-8EE675AD68BB", "Instances": { "Instance": [{ "InnerIpAddress": { "IpAddress": [] }, "ImageId": "m-28ll479ky", "InstanceTypeFamily": "ecs.xn4", "VlanId": "", "NetworkInterfaces": { "NetworkInterface": [{ "MacAddress": "00:16:3e:05:a3:ef", "PrimaryIpAddress": "192.168.0.131", "NetworkInterfaceId": "eni-m5e6niist70q5667h5nn" }] }, "InstanceId": "i-m5ebz1ckaqt75fnzcy9a", "EipAddress": { "IpAddress": "", "AllocationId": "", "InternetChargeType": "" }, "InternetMaxBandwidthIn": 500, "ZoneId": "cn-qingdao-b", "InternetChargeType": "PayByBandwidth", "SpotStrategy": "NoSpot", "StoppedMode": "Not-applicable", "SerialNumber": "3952c3fa-ad4d-4e41-a351-32009ee83901", "IoOptimized": true, "Memory": 1024, "Cpu": 1, "VpcAttributes": { "NatIpAddress": "", "PrivateIpAddress": { "IpAddress": ["192.168.0.131"] }, "VSwitchId": "vsw-m5ev7fvxpi6tno400qxpw", "VpcId": "vpc-m5ebc02fkbkrspyt1fwpg" }, "InternetMaxBandwidthOut": 1, "DeviceAvailable": true, "SecurityGroupIds": { "SecurityGroupId": ["sg-m5eb5k5bjprzv1i1hu5t"] }, "SaleCycle": "", "SpotPriceLimit": 0.0, "AutoReleaseTime": "", "StartTime": "2018-04-16T08:26Z", "InstanceName": "iZm5ebz1ckaqt75fnzcy9aZ", "Description": "", "ResourceGroupId": "", "OSType": "linux", "OSName": "CentOS 6.5 64位", "InstanceNetworkType": "vpc", "PublicIpAddress": { "IpAddress": ["47.104.82.179"] }, "HostName": "iZm5ebz1ckaqt75fnzcy9aZ", "InstanceType": "ecs.xn4.small", "CreationTime": "2018-04-16T08:25Z", "Status": "Running", "ClusterId": "", "Recyclable": false, "RegionId": "cn-qingdao", "GPUSpec": "", "OperationLocks": { "LockReason": [] }, "InstanceChargeType": "PostPaid", "GPUAmount": 0, "ExpiredTime": "2999-09-08T16:00Z" }] } } ``` 这个是调阿里api,返回的数据,我怎么才能任意访问里面的数据, 例如我想获取 这个ip地址"IpAddress": ["47.104.82.179"] 尝试使用以下方法,但这个貌似只是打印,没能访问我要的具体值 ``` func print_json(m map[string]interface{}) { for k, v := range m { switch vv := v.(type) { case string: fmt.Println(k, "is string", vv) case float64: fmt.Println(k, "is float", int64(vv)) case int: fmt.Println(k, "is int", vv) case []interface{}: fmt.Println(k, "is an array:") for i, u := range vv { fmt.Println(i, u) } case nil: fmt.Println(k, "is nil", "null") case map[string]interface{}: fmt.Println(k, "is an map:") print_json(vv) default: fmt.Println(k, "is of a type I don't know how to handle ", fmt.Sprintf("%T", v)) } } } ``` 而且打印结果是这样的。。。 ``` PageNumber is float 1 TotalCount is float 1 PageSize is float 10 RequestId is string D9CA05B0-96B4-43AB-9AAC-77E15A60B4AF Instances is an map: Instance is an array: 0 map[IoOptimized:true Memory:1024 SaleCycle: CreationTime:2018-04-16T08:25Z ImageId:m-28ll479ky InternetChargeType:PayByBandwidth OSName:CentOS 6.5 64位 ZoneId:cn-qingdao-b SecurityGroupIds:map[SecurityGroupId:[sg-m5eb5k5bjprzv1i1hu5t]] SpotPriceLimit:0 InstanceName:iZm5ebz1ckaqt75fnzcy9aZ InstanceNetworkType:vpc Status:Running ClusterId: OperationLocks:map[LockReason:[]] InstanceTypeFamily:ecs.xn4 SpotStrategy:NoSpot InternetMaxBandwidthOut:1 StartTime:2018-04-16T08:26Z OSType:linux GPUSpec: GPUAmount:0 EipAddress:map[IpAddress: AllocationId: InternetChargeType:] InternetMaxBandwidthIn:500 VpcAttributes:map[NatIpAddress: PrivateIpAddress:map[IpAddress:[192.168.0.131]] VSwitchId:vsw-m5ev7fvxpi6tno400qxpw VpcId:vpc-m5ebc02fkbkrspyt1fwpg] RegionId:cn-qingdao VlanId: StoppedMode:Not-applicable Cpu:1 Description: InstanceType:ecs.xn4.small NetworkInterfaces:map[NetworkInterface:[map[MacAddress:00:16:3e:05:a3:ef PrimaryIpAddress:192.168.0.131 NetworkInterfaceId:eni-m5e6niist70q5667h5nn]]] InstanceId:i-m5ebz1ckaqt75fnzcy9a PublicIpAddress:map[IpAddress:[47.104.82.179]] Recyclable:false InstanceChargeType:PostPaid ExpiredTime:2999-09-08T16:00Z InnerIpAddress:map[IpAddress:[]] DeviceAvailable:true AutoReleaseTime: ResourceGroupId: HostName:iZm5ebz1ckaqt75fnzcy9aZ SerialNumber:3952c3fa-ad4d-4e41-a351-32009ee83901] ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

926 次点击  
加入收藏 微博
4 回复  |  直到 2018-04-17 19:50:54
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传