将结构编码为 json Go
Encoding struct to json Go
我在将结构编码为 json 时遇到问题,我的代码是
type MainStructure struct {
Text string json:"text,omitempty"
Array []TestArray json:"test_array,omitmepty"
}
type TestArray struct {
ArrayText string json:"array_text,omitempty"
}
func main() {
Test := MainStructure{
Text: "test",
Array: [
{
ArrayText: "test1"
},
{
ArrayText: "test2"
}
]
}
body := new(bytes.Buffer)
json.NewEncoder(body).Encode(&Test)
fmt.Println(string([]byte(body)))
}
想要的结果应该是
{
"text": "test",
"test_array": [
{
"array_text": "test1"
},
{
"array_text": "test2"
}
]
}
我不介意是 "Marshal" 还是 "encoding/json"
首先,我认为您在创建 struct in go
时出错了,因为您可以轻松地将它们转换为 json。
你应该先制作一个合适的结构,然后 json.marshal(Test)
将其转换为合适的 json,例如:
package main
import (
"encoding/json"
"fmt"
)
func main() {
type TestArray struct {
ArrayText string `json:"array_text,omitempty"`
}
type MainStructure struct {
Text string `json:"text,omitempty"`
Array []TestArray `json:"test_array,omitmepty"`
}
Test := MainStructure{
Text: "test",
Array: []TestArray{
TestArray{ArrayText: "test1"},
TestArray{ArrayText: "test2"},
}}
bytes, err := json.Marshal(Test)
if err != nil {
fmt.Println("eror marshalling")
} else {
fmt.Println(string(bytes))
}
}
上查看
如果您的 objective 只是将结果放入控制台,我无法理解您想要使用 bytes.Buffer
的要点。假设点是:
- 创建一个struct实例(对应一个JSON对象)
- 在屏幕上发射它
以下代码可以帮到您:
package main
import "encoding/json"
import "fmt"
type MainStructure struct {
Text string `json:"text,omitempty"`
Array []TestArray `json:"test_array,omitmepty"`
}
type TestArray struct {
ArrayText string `json:"array_text,omitempty"`
}
func main() {
Test := MainStructure{
Text: "test",
Array: []TestArray{
TestArray{"test1"},
TestArray{"test2"},
},
}
res, _ := json.MarshalIndent(Test, "", "\t")
fmt.Println(string(res))
}
json.MarshalIndent
用于使结果格式漂亮,如果你打扰的话。
要将结构编码为JSON字符串,标准库提供了三种方式:
- 使用
Encoder
将struct 转换为JSON 字符串,然后将其写入io.Writer
。如果您想将 JSON 数据作为 HTTP 请求发送,或将 JSON 字符串保存到文件中,则通常使用此方法。
- 使用
Marshal
简单地将结构转换为字节,可以轻松转换为字符串。
- 使用
MarshalIndent
,其工作方式与 Marshal
相似,只是它还美化了输出。这就是您现在想要解决的问题。
要比较这三种方法,您可以使用此代码 (Go Playground):
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type MainStructure struct {
Text string `json:"text,omitempty"`
Array []TestArray `json:"test_array,omitempty"`
}
type TestArray struct {
ArrayText string `json:"array_text,omitempty"`
}
func main() {
Test := MainStructure{
Text: "test",
Array: []TestArray{
{ArrayText: "test1"},
{ArrayText: "test2"},
},
}
// Using marshal indent
btResult, _ := json.MarshalIndent(&Test, "", " ")
fmt.Println("Using Marshal Indent:\n" + string(btResult))
// Using marshal
btResult, _ = json.Marshal(&Test)
fmt.Println("\nUsing Marshal:\n" + string(btResult))
// Using encoder
var buffer bytes.Buffer
json.NewEncoder(&buffer).Encode(&Test)
fmt.Println("\nUsing Encoder:\n" + buffer.String())
}
输出将如下所示:
Using Marshal Indent:
{
"text": "test",
"test_array": [
{
"array_text": "test1"
},
{
"array_text": "test2"
}
]
}
Using Marshal:
{"text":"test","test_array":[{"array_text":"test1"},{"array_text":"test2"}]}
Using Encoder:
{"text":"test","test_array":[{"array_text":"test1"},{"array_text":"test2"}]}
我在将结构编码为 json 时遇到问题,我的代码是
type MainStructure struct { Text stringjson:"text,omitempty"
Array []TestArrayjson:"test_array,omitmepty"
}type TestArray struct { ArrayText string
json:"array_text,omitempty"
}func main() { Test := MainStructure{ Text: "test", Array: [ { ArrayText: "test1" }, { ArrayText: "test2" } ] } body := new(bytes.Buffer) json.NewEncoder(body).Encode(&Test) fmt.Println(string([]byte(body))) }
想要的结果应该是
{ "text": "test", "test_array": [ { "array_text": "test1" }, { "array_text": "test2" } ] }
我不介意是 "Marshal" 还是 "encoding/json"
首先,我认为您在创建 struct in go
时出错了,因为您可以轻松地将它们转换为 json。
你应该先制作一个合适的结构,然后 json.marshal(Test)
将其转换为合适的 json,例如:
package main
import (
"encoding/json"
"fmt"
)
func main() {
type TestArray struct {
ArrayText string `json:"array_text,omitempty"`
}
type MainStructure struct {
Text string `json:"text,omitempty"`
Array []TestArray `json:"test_array,omitmepty"`
}
Test := MainStructure{
Text: "test",
Array: []TestArray{
TestArray{ArrayText: "test1"},
TestArray{ArrayText: "test2"},
}}
bytes, err := json.Marshal(Test)
if err != nil {
fmt.Println("eror marshalling")
} else {
fmt.Println(string(bytes))
}
}
上查看
如果您的 objective 只是将结果放入控制台,我无法理解您想要使用 bytes.Buffer
的要点。假设点是:
- 创建一个struct实例(对应一个JSON对象)
- 在屏幕上发射它
以下代码可以帮到您:
package main
import "encoding/json"
import "fmt"
type MainStructure struct {
Text string `json:"text,omitempty"`
Array []TestArray `json:"test_array,omitmepty"`
}
type TestArray struct {
ArrayText string `json:"array_text,omitempty"`
}
func main() {
Test := MainStructure{
Text: "test",
Array: []TestArray{
TestArray{"test1"},
TestArray{"test2"},
},
}
res, _ := json.MarshalIndent(Test, "", "\t")
fmt.Println(string(res))
}
json.MarshalIndent
用于使结果格式漂亮,如果你打扰的话。
要将结构编码为JSON字符串,标准库提供了三种方式:
- 使用
Encoder
将struct 转换为JSON 字符串,然后将其写入io.Writer
。如果您想将 JSON 数据作为 HTTP 请求发送,或将 JSON 字符串保存到文件中,则通常使用此方法。 - 使用
Marshal
简单地将结构转换为字节,可以轻松转换为字符串。 - 使用
MarshalIndent
,其工作方式与Marshal
相似,只是它还美化了输出。这就是您现在想要解决的问题。
要比较这三种方法,您可以使用此代码 (Go Playground):
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type MainStructure struct {
Text string `json:"text,omitempty"`
Array []TestArray `json:"test_array,omitempty"`
}
type TestArray struct {
ArrayText string `json:"array_text,omitempty"`
}
func main() {
Test := MainStructure{
Text: "test",
Array: []TestArray{
{ArrayText: "test1"},
{ArrayText: "test2"},
},
}
// Using marshal indent
btResult, _ := json.MarshalIndent(&Test, "", " ")
fmt.Println("Using Marshal Indent:\n" + string(btResult))
// Using marshal
btResult, _ = json.Marshal(&Test)
fmt.Println("\nUsing Marshal:\n" + string(btResult))
// Using encoder
var buffer bytes.Buffer
json.NewEncoder(&buffer).Encode(&Test)
fmt.Println("\nUsing Encoder:\n" + buffer.String())
}
输出将如下所示:
Using Marshal Indent:
{
"text": "test",
"test_array": [
{
"array_text": "test1"
},
{
"array_text": "test2"
}
]
}
Using Marshal:
{"text":"test","test_array":[{"array_text":"test1"},{"array_text":"test2"}]}
Using Encoder:
{"text":"test","test_array":[{"array_text":"test1"},{"array_text":"test2"}]}