Created
December 21, 2018 13:40
-
-
Save eiyaya/6969168933a7fd0200ccb492eab7e3ab to your computer and use it in GitHub Desktop.
Go program that uses json.Unmarshal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type Language struct { | |
Id int | |
Name string | |
} | |
func main() { | |
// String contains two JSON rows. | |
text := "[{\"Id\": 100, \"Name\": \"Go\"}, {\"Id\": 200, \"Name\": \"Java\"}]" | |
// Get byte slice from string. | |
bytes := []byte(text) | |
// Unmarshal string into structs. | |
var languages []Language | |
json.Unmarshal(bytes, &languages) | |
// Loop over structs and display them. | |
for l := range languages { | |
fmt.Printf("Id = %v, Name = %v", languages[l].Id, languages[l].Name) | |
fmt.Println() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsonformatter.org/json-escape