Guest User

Untitled

a guest
Jul 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. )
  8.  
  9. func showMapJSON(basepath string, m map[string]interface{}) {
  10. for k, v := range m {
  11. vrv := reflect.ValueOf(v)
  12. vrt := vrv.Type()
  13.  
  14. switch vrt.Kind() {
  15. case reflect.Float64:
  16. fmt.Printf("%s%s = %f\n", basepath, k, vrv.Float())
  17. case reflect.String:
  18. fmt.Printf("%s%s = %s\n", basepath, k, vrv.String())
  19. case reflect.Slice:
  20. length := vrv.Len()
  21. for i := 0; i < length; i++ {
  22. e := vrv.Index(i)
  23. fmt.Printf("%s%s[%d] = %v\n", basepath, k, i, e)
  24. }
  25. case reflect.Map:
  26. showMapJSON(basepath+k+".", vrv.Interface().(map[string]interface{}))
  27. }
  28.  
  29. }
  30. }
  31. func main() {
  32. var m map[string]interface{}
  33. json.Unmarshal([]byte(`{
  34. "A":3,
  35. "S":"asd",
  36. "Bas":{
  37. "K":1,
  38. "C":[1,"2",3.3],
  39. "RR":{
  40. "m":1
  41. }
  42. },
  43. "arr":[1,"a2z",3.124908]
  44. }`), &m)
  45. showMapJSON("json.", m)
  46. }
Add Comment
Please, Sign In to add comment