SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import "fmt"
  4. import "encoding/json"
  5.  
  6.  
  7. type Person1 struct {
  8.     Name string
  9.     Age  string
  10. }
  11.  
  12. type Person2 struct {
  13.     Name string
  14.     Age  int    
  15. }
  16.  
  17. type Person3 struct {
  18.     Name string
  19.     Age  int    `json:",string"`
  20. }
  21.  
  22.  
  23. type Person4 struct {
  24.     Name string `json:"nombre"`
  25.     Age  int    `json:"edad,string"`
  26. }
  27.  
  28. type Person5 struct {
  29.     FirstName         string   `json:"first_name"`
  30.     CurrentAge        float64  `json:"current_age,string"`
  31. }
  32.  
  33. func main() {
  34.  
  35.     // Parse json with strings to string properties
  36.     str := `[{"name": "isidro","age": "24"},{"name": "Abelardo","age": "24"}]`
  37.     person1 := []Person1{}
  38.     json.Unmarshal([]byte(str), &person1)
  39.     fmt.Println("Person 1:", person1)
  40.  
  41.     // Parse json with strings and numbers to string properties
  42.     str = `[{"name": "isidro","age": "24"},{"name": "Abelardo","age": 24}]`
  43.     person2 := []Person2{}
  44.     json.Unmarshal([]byte(str), &person2)
  45.     fmt.Println("Person 2:", person2)
  46.  
  47.  
  48.     str = `[{"name": "isidro","age": "24"},{"name": "Abelardo","age": "24"}]`
  49.     person3 := []Person3{}
  50.     json.Unmarshal([]byte(str), &person3)
  51.     fmt.Println("Person 3:", person3)
  52.  
  53.  
  54.     str = `[{"nombre": "isidro","edad": "24"},{"nombre": "Abelardo","edad": "24"}]`
  55.     person4 := []Person4{}
  56.     json.Unmarshal([]byte(str), &person4)
  57.     fmt.Println("Person 4:", person4)
  58.  
  59.     str = `[{"first_name": "isidro","current_age": "24"},{"first_name": "Abelardo","current_age": "24"}]`
  60.     person5 := []Person5{}
  61.     json.Unmarshal([]byte(str), &person5)
  62.     fmt.Println("Person 5:", person5)
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top