用GO语言反序列化"{ "code": 0, "msg": "success", "data": { "id": 1, "createdAt": "2023-05-19T09:58:56.776+08:00", "updatedAt": "2023-06-09T14:47:50.188+08:00", "transparency": 0, "ownerID": 1, "ownerType": "ps_lakes" }"拿出id
时间: 2023-07-19 21:54:45 浏览: 111
您可以使用以下代码将JSON字符串反序列化为Go语言的结构体,并获取其中的id字段:
```go
package main
import (
"encoding/json"
"fmt"
)
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type Data struct {
ID int `json:"id"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Transparency int `json:"transparency"`
OwnerID int `json:"ownerID"`
OwnerType string `json:"ownerType"`
}
func main() {
jsonStr := `{
"code": 0,
"msg": "success",
"data": {
"id": 1,
"createdAt": "2023-05-19T09:58:56.776+08:00",
"updatedAt": "2023-06-09T14:47:50.188+08:00",
"transparency": 0,
"ownerID": 1,
"ownerType": "ps_lakes"
}
}`
var resp Response
if err := json.Unmarshal([]byte(jsonStr), &resp); err != nil {
panic(err)
}
if data, ok := resp.Data.(map[string]interface{}); ok {
if id, ok := data["id"].(float64); ok {
fmt.Println("id:", int(id))
}
}
}
```
输出:
```
id: 1
```
阅读全文