用Go语言取出id,xcoor,ycoor的值{ "code": 0, "msg": "success", "data": { "id": 22, "createdAt": "2023-06-12T09:58:49.141+08:00", "updatedAt": "2023-06-12T09:58:49.145+08:00", "deletedAt": null, "pipeID": "L0133028200000022", "xCoor": 4.5, "yCoor": 9, "name": "", "systemID": "1", "pipeCategory": 0, "pipeLength": 0, "roadName": "", "inJuncID": "P073302820000001", "outJuncID": "A023302820000001", "inElev": 0, "outElev": 0, "shapeType": 0, "invertSilphon": 0, "shapeData1": 0, "shapeData2": 0, "shapeData3": 0, "shapeData4": 0, "shapeXYData": 0, "material": 0, "otherMaterial": "", "roughness": 0, "dataSource": 0, "otherSource": "", "reportUnit": "", "pressureType": 0, "wallThickness": 0, "linerMaterial": 0, "linerThickness": 0, "jointType": 0, "connType": 0, "pipeSlope": 0, "ownership": 0, "shapeArea": 0, "selfWashing": 0, "rainSewaMix": 0, "originStruct": 0, "constrMethod": 0, "trenchlessMethod": 0, "status": 0, "remark": "", "diameter": 0, "flowDir": 0, "pipeClass": 0, "pipeClassOther": "", "points": "[[121.291165,30.173354],[121.289325,30.1733],[121.289145,30.172799],[121.288794,30.172484],[121.287953,30.169528],[121.291075,30.169468]]", "code": "330282000000" } }
时间: 2023-07-19 16:48:54 浏览: 177
可以通过使用 Go 语言中的 JSON 解析库将上述 JSON 格式的字符串转换为对应的 Go 语言中的结构体,然后再取出其中的 id、xCoor 和 yCoor 的值。以下是一个示例代码:
```go
package main
import (
"encoding/json"
"fmt"
)
type Data struct {
Id int `json:"id"`
XCoor float64 `json:"xCoor"`
YCoor float64 `json:"yCoor"`
}
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data Data `json:"data"`
}
func main() {
jsonString := `{"code":0,"msg":"success","data":{"id":22,"createdAt":"2023-06-12T09:58:49.141+08:00","updatedAt":"2023-06-12T09:58:49.145+08:00","deletedAt":null,"pipeID":"L0133028200000022","xCoor":4.5,"yCoor":9,"name":"","systemID":"1","pipeCategory":0,"pipeLength":0,"roadName":"","inJuncID":"P073302820000001","outJuncID":"A023302820000001","inElev":0,"outElev":0,"shapeType":0,"invertSilphon":0,"shapeData1":0,"shapeData2":0,"shapeData3":0,"shapeData4":0,"shapeXYData":0,"material":0,"otherMaterial":"","roughness":0,"dataSource":0,"otherSource":"","reportUnit":"","pressureType":0,"wallThickness":0,"linerMaterial":0,"linerThickness":0,"jointType":0,"connType":0,"pipeSlope":0,"ownership":0,"shapeArea":0,"selfWashing":0,"rainSewaMix":0,"originStruct":0,"constrMethod":0,"trenchlessMethod":0,"status":0,"remark":"","diameter":0,"flowDir":0,"pipeClass":0,"pipeClassOther":"","points":"[[121.291165,30.173354],[121.289325,30.1733],[121.289145,30.172799],[121.288794,30.172484],[121.287953,30.169528],[121.291075,30.169468]]","code":"330282000000"}}`
var response Response
err := json.Unmarshal([]byte(jsonString), &response)
if err != nil {
fmt.Println("Error parsing JSON:", err)
return
}
fmt.Println("id:", response.Data.Id)
fmt.Println("xCoor:", response.Data.XCoor)
fmt.Println("yCoor:", response.Data.YCoor)
}
```
运行该代码将输出:
```
id: 22
xCoor: 4.5
yCoor: 9
```
阅读全文