Home  >  Article  >  Backend Development  >  How to convert structure to JSON string using json.Marshal function in golang

How to convert structure to JSON string using json.Marshal function in golang

PHPz
PHPzOriginal
2023-11-18 08:05:301382browse

How to convert structure to JSON string using json.Marshal function in golang

How to use the json.Marshal function in golang to convert a structure into a JSON string

In modern software development, data transmission and storage often use JSON ( JavaScript Object Notation) format. In Go language, we can use the json.Marshal function to convert a structure into a JSON string.

The json.Marshal function is located in the encoding/json package. Its function signature is as follows:

func Marshal(v interface{}) ([]byte, error)

Among them, v is the structure variable to be converted to a JSON string, and the function returns a byte slice and an error. If the conversion is successful, the returned byte slice is a representation of the JSON string; if the conversion fails, a non-nil error is returned.

Below we use a specific example to demonstrate how to use the json.Marshal function to convert a structure into a JSON string.

package main

import (
    "encoding/json"
    "fmt"
)

type Student struct {
    Name  string
    Age   int
    Score float64
}

func main() {
    student := Student{
        Name:  "Alice",
        Age:   20,
        Score: 89.5,
    }

    // 将结构体转换为JSON字符串
    jsonStr, err := json.Marshal(student)
    if err != nil {
        fmt.Println("转换失败:", err)
        return
    }

    fmt.Println(string(jsonStr))
}

Run the above code, the output result is:

{"Name":"Alice","Age":20,"Score":89.5}

In the above code, we define a Student structure, then create a student variable and assign it an initial value. Next, we call the json.Marshal function to convert student to a JSON string and get the jsonStr variable. Finally, we use fmt.Println to print out the jsonStr.

It should be noted that the json.Marshal function converts the field names of the structure into JSON attribute names and converts the attribute values ​​into the appropriate JSON type. By default, attribute names in the converted JSON string are all lowercase letters. If you want to keep the original case of the field name, you can use the json directive in the Tag of the structure field to set it.

For example:

type Student struct {
    Name  string `json:"name"`
    Age   int    `json:"age"`
    Score float64 `json:"score"`
}

In this way, the attribute names in the converted JSON string will be consistent with the names of the original structure fields.

To summarize, we can convert the structure into a JSON string by using the json.Marshal function. This function is very convenient and can automatically handle complex data type conversion and field name correspondence. With this function, we can easily handle the transmission and storage of JSON data in Go language.

The above is the detailed content of How to convert structure to JSON string using json.Marshal function in golang. 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