Skip to content

Instantly share code, notes, and snippets.

@isidroamv
Created March 4, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isidroamv/c5883eff156bfc183ad0 to your computer and use it in GitHub Desktop.
Save isidroamv/c5883eff156bfc183ad0 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "encoding/json"
type Person1 struct {
Name string
Age string
}
type Person2 struct {
Name string
Age int
}
type Person3 struct {
Name string
Age int `json:",string"`
}
type Person4 struct {
Name string `json:"nombre"`
Age int `json:"edad,string"`
}
type Person5 struct {
FirstName string `json:"first_name"`
CurrentAge float64 `json:"current_age,string"`
}
func main() {
// Parse json with strings to string properties
str := `[{"name": "isidro","age": "24"},{"name": "Abelardo","age": "24"}]`
person1 := []Person1{}
json.Unmarshal([]byte(str), &person1)
fmt.Println("Person 1:", person1)
// Parse json with strings and numbers to string properties
str = `[{"name": "isidro","age": "24"},{"name": "Abelardo","age": 24}]`
person2 := []Person2{}
json.Unmarshal([]byte(str), &person2)
fmt.Println("Person 2:", person2)
str = `[{"name": "isidro","age": "24"},{"name": "Abelardo","age": "24"}]`
person3 := []Person3{}
json.Unmarshal([]byte(str), &person3)
fmt.Println("Person 3:", person3)
str = `[{"nombre": "isidro","edad": "24"},{"nombre": "Abelardo","edad": "24"}]`
person4 := []Person4{}
json.Unmarshal([]byte(str), &person4)
fmt.Println("Person 4:", person4)
str = `[{"first_name": "isidro","current_age": "24"},{"first_name": "Abelardo","current_age": "24"}]`
person5 := []Person5{}
json.Unmarshal([]byte(str), &person5)
fmt.Println("Person 5:", person5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment