How to read JSON data from io.Reader using bufio in Golang?

WBOY
Release: 2024-06-03 15:48:02
Original
948 people have browsed it

How to use bufio to read JSON data from io.Reader in Go? Use bufio.NewReader to create a buffered io.Reader. Use bufio.ReadBytes to read JSON until a delimiter (usually a newline character) is encountered. Use the encoding/json package to parse JSON data.

如何在 Golang 中使用 bufio 从 io.Reader 中读取 JSON 数据?

How to use bufio in Golang to read JSON data from io.Reader

Reading JSON data is in Golang Common tasks. To read JSON from io.Reader you can use the bufio package. This is a simple tutorial demonstrating how to read JSON data from io.Reader using bufio.

Install bufio package

import "bufio"
Copy after login

Create io.Reader
To read JSON from io.Reader , you need to create a io.Reader first. You can use os.Stdin to use standard input or create a file to read from.

Use bufio.NewReader to create a buffered io.Reader
bufio The package provides the NewReader function, which can create a buffered io.Reader. Buffered io.Reader. This can improve read performance on small files or network connections.

reader := bufio.NewReader(in)
Copy after login

Read JSON using bufio.ReadBytes
bufio package provides a function named ReadBytes, which can be read from io.Reader reads until a delimiter is encountered. For JSON data, the delimiter is usually the newline character ('\n').

line, err := reader.ReadBytes('\n')
if err != nil {
    // 处理错误
}
Copy after login

Parsing JSON
After reading the JSON lines, you can use the encoding/json package to parse the JSON data.

var data map[string]interface{}
err = json.Unmarshal(line, &data)
if err != nil {
    // 处理错误
}
Copy after login

Practical case
The following is a complete example of how to use bufio to read JSON data from io.Reader.

import (
    "bufio"
    "encoding/json"
    "fmt"
    "os"
)

func main() {
    // 使用标准输入创建 io.Reader
    reader := bufio.NewReader(os.Stdin)

    // 读取 JSON 数据直到遇到换行符
    line, err := reader.ReadBytes('\n')
    if err != nil {
        fmt.Println("Error reading JSON:", err)
        os.Exit(1)
    }

    // 解析 JSON 数据
    var data map[string]interface{}
    err = json.Unmarshal(line, &data)
    if err != nil {
        fmt.Println("Error parsing JSON:", err)
        os.Exit(1)
    }

    // 打印数据
    fmt.Println(data)
}
Copy after login

The above is the detailed content of How to read JSON data from io.Reader using bufio in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!