用go语言提取{"access_token":"hy.patrol.1.a1b421bb-46fd-4460-bddf-777eaefc68b4","token_type":"bearer","refresh_token":"hy.patrol.1.d669276c-446c-4654-9f12-837a9af1bb33","expires_in":3558,"scope":"all","user_info":{"username":"H02110"}}中的"access_token":"hy.patrol.1.a1b421bb-46fd-4460-bddf-777eaefc68b4"并保持成json
时间: 2023-05-27 12:04:41 浏览: 97
代码实现:
```go
package main
import (
"encoding/json"
"fmt"
)
func main() {
originalJson := `{"access_token":"hy.patrol.1.a1b421bb-46fd-4460-bddf-777eaefc68b4","token_type":"bearer","refresh_token":"hy.patrol.1.d669276c-446c-4654-9f12-837a9af1bb33","expires_in":3558,"scope":"all","user_info":{"username":"H02110"}}`
var result map[string]interface{}
err := json.Unmarshal([]byte(originalJson), &result)
if err != nil {
fmt.Println("Unmarshal error:", err)
return
}
access_token, ok := result["access_token"]
if !ok {
fmt.Println("access_token not found")
return
}
newJson := map[string]interface{}{
"access_token": access_token,
}
newBytes, err := json.Marshal(newJson)
if err != nil {
fmt.Println("Marshal error:", err)
return
}
fmt.Println(string(newBytes))
}
```
输出结果:
```
{"access_token":"hy.patrol.1.a1b421bb-46fd-4460-bddf-777eaefc68b4"}
```
阅读全文