Convert structure to JSON string using json.Marshal function
Jul 24, 2023 pm 12:54 PMUse the json.Marshal function to convert a structure into a JSON string
In Go language, you can use the json.Marshal function to convert a structure into a JSON string. A structure is a data type composed of multiple fields, and JSON is a commonly used lightweight data exchange format. Converting structures to JSON strings makes it easy to exchange data between different systems.
The following is a sample code:
package main import ( "encoding/json" "fmt" ) // 定義一個結(jié)構(gòu)體 type Person struct { Name string `json:"name"` Age int `json:"age"` Gender string `json:"gender"` } func main() { // 創(chuàng)建一個Person對象 p := Person{ Name: "張三", Age: 20, Gender: "男", } // 將結(jié)構(gòu)體轉(zhuǎn)換為JSON字符串 jsonData, err := json.Marshal(p) if err != nil { fmt.Println("轉(zhuǎn)換JSON失敗:", err) return } // 輸出JSON字符串 fmt.Println(string(jsonData)) }
In the above code, we first define a structure named Person, which contains three fields: Name, Age and Gender . Field names can be specified when converting to JSON by adding the json:"xxx"
tag after the field.
Next, a Person object is created in the main
function and its fields are assigned values. Then, call the json.Marshal
function to convert the Person structure into a JSON string. This function will return a slice of type []byte
and an error. If the conversion is successful, jsonData
will store the converted JSON string; if the conversion fails, err
will store the error information.
Finally, we use the fmt.Println
function to output the converted JSON string. In this example, the output result is {"name":"Zhang San","age":20,"gender":"male"}
.
Using the json.Marshal function to convert a structure into a JSON string is one of the commonly used operations in the Go language. Through the above examples, I believe you have grasped the basic methods of this process. In actual development, please adjust the definition of the structure and fields, as well as the format requirements of the JSON string according to actual needs.
The above is the detailed content of Convert structure to JSON string using json.Marshal function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

When using a computer to input English, sometimes we encounter the difference between full-width English letters and half-width English letters. Full-width English letters refer to the characters input by pressing the Shift key and the English letter key combination when the input method is Chinese mode. They occupy a full-width character width. Half-width English letters refer to characters input directly when the input method is English mode, and they occupy half a character width. In some cases, we may need to convert full-width English letters to half-width letters. Here is a simple tutorial: First, open a text editor or any

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

What is the difference between full-width characters and half-width characters? In our daily life, we often encounter the two concepts of full-width characters and half-width characters, especially in input methods, typesetting, printing, etc., which involve the use of these two characters. So, what is the difference between full-width characters and half-width characters? Let us discuss this issue below. First of all, full-width characters and half-width characters were concepts originally introduced by Chinese typewriters. The so-called half-width characters are characters that occupy half the width of a character, usually referring to English characters and Arabic numerals. Full-width characters occupy
