英文:
how to fetch data from another api with body raw json
问题
我有一个默认的原始 JSON 数据,想要将其粘贴到一个结构体中,以便可以自动获取数据并保存到结构体中。
原始 JSON 数据
{
"jsonrpc": "2.0",
"params": {}
}
来自 API 的响应
{
"jsonrpc": "2.0",
"id": null,
"result": {
"status": 200,
"response": [
{
"service_id": 1129,
"service_name": "Adobe Illustrator",
"service_category_id": 28,
"service_category_name": "License Software",
"service_type_id": 25,
"service_type_name": "Software",
"create_date": "2020-03-09 03:47:44"
}
],
"message": "Done All User Returned"
}
}
我想将其放入存储库文件中,以便可以自动获取数据。
存储库文件
// 发送请求
resp, err := http.Get("查看 API 响应示例")
if err != nil {
fmt.Println("请求无响应")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) // 响应体是 []byte 类型
if err != nil {
return err
}
// 将已获取的数据适配到结构体中
var result models.OdooRequest
if err := json.Unmarshal(body, &result); err != nil {
fmt.Println("无法解析 JSON")
}
for _, rec := range result.Response {
fmt.Println(rec.ServiceName)
}
return err
在获取后,将其适配到一个结构体中。
结构体
type OdooRequest struct {
Response []UpsertFromOdooServices
}
希望这能帮到你!
英文:
i have default body raw json and want to paste it into a struct so it can fetch data automatically and save it into a struct
Body Raw Json
{
"jsonrpc": "2.0",
"params": {
}
}
Response from api
{
"jsonrpc": "2.0",
"id": null,
"result": {
"status": 200,
"response": [
{
"service_id": 1129,
"service_name": "Adobe Illustrator",
"service_category_id": 28,
"service_category_name": "License Software",
"service_type_id": 25,
"service_type_name": "Software",
"create_date": "2020-03-09 03:47:44"
},
],
"message": "Done All User Returned"
}
}
I want to put it in the repository file so I can get data automatically
Repo file
// Get request
resp, err := http.Get("look at API Response Example")
if err != nil {
fmt.Println("No response from request")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) // response body is []byte
if err != nil {
return err
}
// data that already fetch accomadate to struct
var result models.OdooRequest
if err := json.Unmarshal(body, &result); err != nil {
fmt.Println("Can not unmarshal JSON")
}
for _, rec := range result.Response {
fmt.Println(rec.ServiceName)
}
return err
after being fetched then accommodated into a struct
struct
type OdooRequest struct {
Response []UpsertFromOdooServices
}
答案1
得分: 0
当然,这是一个大致的方法来发送请求并读取响应:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type OdooRequest struct {
Result struct {
Status int `json:"status"`
Response []struct {
ServiceID int `json:"service_id"`
ServiceName string `json:"service_name"`
ServiceCategoryID int `json:"service_category_id"`
ServiceCategoryName string `json:"service_category_name"`
ServiceTypeID int `json:"service_type_id"`
ServiceTypeName string `json:"service_type_name"`
CreateDate string `json:"create_date"`
} `json:"response"`
Message string `json:"message"`
} `json:"result"`
}
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
resp, err := http.Post(
"ADD_URL_HERE",
"application/json",
bytes.NewBufferString(`{"jsonrpc": "2.0","params": {}}`),
)
if err != nil {
return err
}
defer resp.Body.Close()
var odooResp OdooRequest
if err := json.NewDecoder(resp.Body).Decode(&odooResp); err != nil {
return err
}
for _, rec := range odooResp.Result.Response {
fmt.Println(rec.ServiceName)
}
return nil
}
请将ADD_URL_HERE
替换为实际的URL。
英文:
Sure, here's a rough way to make that request and read the response:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type OdooRequest struct {
Result struct {
Status int `json:"status"`
Response []struct {
ServiceID int `json:"service_id"`
ServiceName string `json:"service_name"`
ServiceCategoryID int `json:"service_category_id"`
ServiceCategoryName string `json:"service_category_name"`
ServiceTypeID int `json:"service_type_id"`
ServiceTypeName string `json:"service_type_name"`
CreateDate string `json:"create_date"`
} `json:"response"`
Message string `json:"message"`
} `json:"result"`
}
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
resp, err := http.Post(
"ADD_URL_HERE",
"application/json",
bytes.NewBufferString(`{"jsonrpc": "2.0","params": {}}`),
)
if err != nil {
return err
}
defer resp.Body.Close()
var odooResp OdooRequest
if err := json.NewDecoder(resp.Body).Decode(&odooResp); err != nil {
return err
}
for _, rec := range odooResp.Result.Response {
fmt.Println(rec.ServiceName)
}
return nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论