【问题标题】:Read data from a JSON file and send it as a Post request从 JSON 文件中读取数据并将其作为 Post 请求发送
【发布时间】:2021-08-09 21:11:17
【问题描述】:

如何从 json 文件中读取数据并将其作为发布请求发送到 uri 端点。 我目前正在学习 Go 语言并在做我的第一个学习项目。

这是我的示例数据

// parent.json

{"name":"Jade Copnell","age":16,"gender":"Agender","occupation":"Account Representative II","numbers":"178-862-5967","children":{"name":"Kayne Belsham","age":38,"gender":"Genderqueer","occupation":"Clinical Specialist","interest":"Re-engineered discrete methodology","number":"145-355-4123"},"friends":{"name":"Stephi Aries","age":74,"gender":"Genderqueer","occupation":"Senior Sales Associate","numbers":"873-726-1453","interests":"Self-enabling systematic function","methow":"24/7"}}

这是我写的,当我运行下面的脚本时,我倾向于得到一个类似于下面的数据作为输出,我也会得到空数据发送到数据库。

"{\"name\":\"Jade Copnell\",\"age\":16,\"gender\":\"Agender\",\"occupation\":\"Account Representative II\",\"numbers\":\"178-862-5967\",\"children\":{\"name\":\"Kayne Belsham\",\"age\":38,\"gender\":\"Genderqueer\",\"occupation\":\"Clinical Specialist\",\"interest\":\"Re-engineered discrete methodology\",\"number\":\"145-355-4123\"},\"friends\":{\"name\":\"Stephi Aries\",\"age\":74,\"gender\":\"Genderqueer\",\"occupation\":\"Senior Sales Associate\",\"numbers\":\"873-726-1453\",\"interests\":\"Self-enabling systematic function\",\"methow\":\"24/7\"}}"
func main() {
    // Open the file.
    f, _ := os.Open("./go_data/parent.json")
    // Create a new Scanner for the file.
    scanner := bufio.NewScanner(f)
    // Loop over all lines in the file and print them.
    for scanner.Scan() {
        responseBody := scanner.Text()

        postBody, _ := json.Marshal(responseBody)
        //fmt.Println(postBody)
        time.Sleep(2 * time.Second)
        webBody := bytes.NewBuffer(postBody)
        // fmt.Println(webBody)

        resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", webBody)

        if err != nil {
            log.Fatalf("An Error Occured %v", err)
        }
        time.Sleep(2 * time.Second)
        defer resp.Body.Close()
    }
}

【问题讨论】:

  • 输入文件有一个json对象,还是每一行一个json对象?代码看起来每一行都有一个 JSON 对象,但你编组它,这不是必需的
  • 输入文件的每一行包含一个 json 对象。如果我不编组它,我将无法将数据传递给 http.Post。
  • Marshal 将结构转换为 json。如果文件已经包含 json,则不要编组它。您可以直接发送f 发帖。

标签: json go post http-post


【解决方案1】:

如果您改为这样做会怎样。 http.Post 的第三个参数是一个 io.Reader 接口——你的文件“f”实现了它。

package main

import (
    "bufio"
    "log"
    "net/http"
    "os"
    "time"
)

func main() {
    // Open the file.
    f, _ := os.Open("./go_data/parent.json")

    resp, err := http.Post("http://127.0.0.1:5000/v1/parent", "application/json", f)

    if err != nil {
        log.Fatalf("An Error Occured %v", err)
    }
    time.Sleep(2 * time.Second)
    defer resp.Body.Close()
}

【讨论】:

  • 这里不需要扫描仪。只需打开文件并传递f 即可发布。
  • 如果我想遍历 json 文件的每一行。这种方法还能用吗?
  • 我不确定你想做什么。如果您想以某种方式更手动地处理 JSON 数据,那么我建议您研究一下:pkg.go.dev/encoding/json#Decoder.Token
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 2016-11-22
相关资源
最近更新 更多