Home > Backend Development > Golang > golang json to string

golang json to string

WBOY
Release: 2023-05-13 11:05:07
Original
1977 people have browsed it

Go is a modern programming language designed with simplicity, efficiency and ease of use in mind. One of the features of the Go language is its ability to easily handle various data formats, including JSON.

In Go, JSON is a very common data format, and we often need to convert JSON data into strings. In this article, we will introduce how to convert JSON to string using Go language.

First of all, we need to know that there are two common JSON processing methods in Go language: JSON encoding and JSON decoding.

JSON encoding converts Go data types into JSON strings, while JSON decoding parses JSON strings into Go data types. In this article, we will focus on the implementation of JSON encoding.

The method to convert JSON to string using Go language is very simple. We can use the json.Marshal() function in the standard library to achieve this.

The use of this function is very simple, we only need to pass the Go data type to be encoded as a parameter to the Marshal() function. For example, suppose we have the following JSON data:

{
    "name": "Jack",
    "age": 25,
    "isStudent": true,
    "hobbies": ["reading", "swimming", "traveling"]
}
Copy after login

We can encode it into a string using the following code:

import (
    "encoding/json"
    "fmt"
)

func main() {
    data := map[string]interface{}{
        "name":      "Jack",
        "age":       25,
        "isStudent": true,
        "hobbies":   []string{"reading", "swimming", "traveling"},
    }

    result, err := json.Marshal(data)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(result))
}
Copy after login

Store the above code into main.goFile and execute, we will get the following output:

{"age":25,"hobbies":["reading","swimming","traveling"],"isStudent":true,"name":"Jack"}
Copy after login

When using the json.Marshal() function to convert JSON data into a string, it should be noted that we need to convert the JSON data Stored in a variable of type interface{}, and since the key of JSON is a string type, we use map[string]interface{} to represent the JSON object.

In addition, it should be noted that when we use the json.Marshal() function to encode data into JSON, the exported variables in Go (that is, variables with the first letter capitalized) will automatically ) are converted into key names starting with an uppercase letter in JSON, while unexported variables (that is, variables with the first letter in lowercase) will not be encoded.

For example, if we define the following structure:

type User struct {
    Name      string
    Age       int
    IsStudent bool
    Hobbies   []string
}
Copy after login

Then, when converting the structure to a JSON string, we need to convert it to map[string ]interface{} type, otherwise Go's compiler will not be able to convert it to a JSON string.

In summary, it is very simple to convert JSON to a string using Go language. We only need to use the json.Marshal() function. This function can convert any Go data type to a JSON string. Of course, when using this function, we need to note that the JSON key name must be of string type, otherwise the encoding will fail.

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

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