用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,owner_type
时间: 2023-07-12 14:00:23 浏览: 41
可以定义一个结构体来对应这段JSON数据,然后使用Go语言内置的encoding/json包中的Unmarshal函数来对JSON数据进行反序列化,具体实现如下:
```go
import (
"encoding/json"
"fmt"
)
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
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"`
} `json:"data"`
}
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
err := json.Unmarshal([]byte(jsonStr), &resp)
if err != nil {
panic(err)
}
fmt.Printf("id: %d, owner_type: %s", resp.Data.ID, resp.Data.OwnerType)
}
```
运行以上代码输出如下:
```
id: 1, owner_type: ps_lakes
```
可以看到,通过反序列化后,我们可以轻松地获取到JSON数据中的id和owner_type字段的值。