```
{
"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]
```
找到一款神器 https://mholt.github.io/json-to-go/
```
fmt.Println(r.Instances.Instance[0].PublicIPAddress.IPAddress[0])
```
#1
更多评论