This repository demonstrates how to decode JSON strings into Go structs using Go's standard library package encoding/json
. It showcases how to unmarshal JSON data into Go structures and perform operations on the decoded data.
- This example covers basic JSON decoding using Go structs and the `json.Unmarshal` function.
- It demonstrates how to convert a JSON string into a Go struct, handle errors during decoding, and marshal the struct back into a formatted JSON string.
package main
import (
"encoding/json"
"fmt"
)
type Person1 struct {
Name string
Age int
Country string
}
func main() {
// This example demonstrates how to convert a JSON string into a Go struct using json.Unmarshal
jsonString := `{"Name":"John","Age":30,"Country":"USA"}`
var person Person1
err := json.Unmarshal([]byte(jsonString), &person)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
// Marshal the struct back to JSON format
jsonOutput, err := json.MarshalIndent(person, "", " ")
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
fmt.Println(string(jsonOutput))
}
-
Make sure you have Go installed. If not, you can download it from here.
-
Clone this repository:
git clone /~https://github.com/Rapter1990/go_sample_examples.git
-
Navigate to the
002_basic_decoding_json
directory:cd go_sample_examples/030_json/002_basic_decoding_json
-
Run the Go program:
go run 002_basic_decoding_json.go
When you run the program, you should see the following output:
{
"Name": "John",
"Age": 30,
"Country": "USA"
}