Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"encoding/json"
)
/* terms:
- Marshaling = Encoding
links:
- https://www.sohamkamani.com/golang/json/
*/
type Person struct {
Name string `json:"username"`
}
func main() {
myDocJSON := `[{"username":"Abdulrahman"},{"username":"Mary"}]`
var persons []Person
json.Unmarshal([]byte(myDocJSON), &persons)
fmt.Println(persons[1])
unstructuredJSON := `{"birds":{"pigeon":"likes to perch on
rocks","eagle":"bird of prey"},"animals":"none"}`
var data map[string]interface{}
json.Unmarshal([]byte(unstructuredJSON), &data)
birds := data["birds"].(map[string]interface{})
fmt.Println(birds)
// Marshaling
personJSON := &Person{
Name : "Abdulrahman",
}
marshalledPersons, _ := json.Marshal([]*Person{personJSON,personJSON})
fmt.Println(string(marshalledPersons))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment