Home  >  Article  >  Backend Development  >  How to convert golang data to json

How to convert golang data to json

PHPz
PHPzOriginal
2023-04-24 09:10:282552browse

With the rapid development of the Internet, data processing and transmission have become increasingly important. JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used on the Internet to transmit data in a text format that is easy to read and write. As an efficient programming language, golang also provides a way to easily convert data into JSON format.

In golang, data can be converted into JSON format by writing code. Specifically, there are the following two methods:

Method 1: Use the standard library encoding/json

The standard library encoding/json provides methods for encoding and decoding JSON format. First we need to define a custom structure, the fields of which need to be marked with json tags:

type Person struct {
Name string json:"name"
Age int json:"age"
}

The json tag here specifies the name of the field in JSON. Next, we can convert the Person type data into JSON format by generating a JSON byte array:

import (
"encoding/json"
"fmt"
)

func main() {
person := Person{Name: "Tom", Age: 18}
data, err := json.Marshal(person)
if err != nil {

fmt.Println("JSON Marshalling failed:", err)
return

}
fmt.Printf("JSON data:\n%s\n", data)
}

Output result:

JSON data:
{"name":"Tom","age":18}

We can also use the Unmarshal function to decode JSON into a structure:

func main() {
jsonStr := {"name":"Tom","age":18}

var person Person
if err := json.Unmarshal([]byte (jsonStr), &person); err != nil {

fmt.Println("JSON Unmarshalling failed:", err)
return

}

fmt.Printf("Decoded Person data: % v\n", person)
}

Output result:

Decoded Person data: {Name:Tom Age:18}

Method 2: Use a third-party library

Use a third-party library to convert the data Formatting to JSON is also very simple. Among the more popular ones are libraries such as gjson and jsoniter. Here we take jsoniter as an example.

First define the custom structure, the same as method 1.

Next, we can use the Marshal function of the jsoniter library to convert the Person type data into JSON format, similar to the standard library:

import (
"github.com/json- iterator/go"
"fmt"
)

func main() {
// define struct
person := Person{Name: "Tom", Age: 18}

// convert data to json format
data, err := jsoniter.Marshal(person)
if err != nil {

fmt.Println("JSON Marshalling failed:", err)
return

}

fmt.Printf("JSON data:\n%s\n", data)
}

Output result:

JSON data:
{"name":"Tom ","age":18}

Similarly, we can also use the Unmarshal function of the jsoniter library to decode the data converted from JSON format:

func main() {
jsonStr := {"name":"Tom","age":18}

var person Person
if err := jsoniter.Unmarshal([]byte(jsonStr), &person); err != nil {

fmt.Println("JSON Unmarshalling failed:", err)
return

}

fmt.Printf("Decoded Person data: % v\n", person)
}

Output Result:

Decoded Person data: {Name:Tom Age:18}

Summary

In golang, we can use the standard library encoding/json to convert the data into JSON format, you can also use third-party libraries to achieve the same conversion. Different libraries differ in some aspects, so you can choose the appropriate library based on your actual situation. No matter which method is used, converting JSON format is very simple.

The above is the detailed content of How to convert golang data to json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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