Advertisement
vyoumans

Untitled

Jan 29th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. // I am getting an error around the unmarshal statement...
  2. // can someone give me a hint please...
  3. // I am just a newbie...
  4.  
  5. package main
  6.  
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "io/ioutil"
  12. "log"
  13. "net/http"
  14. )
  15.  
  16. const (
  17. url01 = "http://testrest01.trainscheds.com/api/v2/VYTEST02-B.json"
  18. url02 = "http://ts04.trainscheds.com/api/v4/rest_brun_rates_json"
  19. )
  20.  
  21. type Message struct {
  22. TIMETEXT string
  23. Body string
  24. title string
  25. }
  26.  
  27. func main() {
  28. // input := `[{"Body":"<p>THIS IS A TEST OF 1</p>\n","TIMETEXT":"12:14","title":"TEST01"},{"Body":"<p>this is the second test item</p>\n","TIMETEXT":"14:23","title":"TEST02"},{"Body":"<p>this is the third test item</p>\n","TIMETEXT":"16:23","title":"TEST03"},{"Body":"<p>The forth attempt to make this work properly</p>\n","TIMETEXT":"16:23","title":"TEST04"},{"Body":"<p>THIS IS THE 5TH ATTEMPT TO MAKE THIS WORK PROPERLY</p>\n","TIMETEXT":"16:34","title":"TEST05"}]`
  29. res, err := http.Get(url01)
  30. //res, err := http.Get(url02)
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34.  
  35. input, err := ioutil.ReadAll(res.Body)
  36.  
  37. var out bytes.Buffer
  38. if err := json.Indent(&out, []byte(input), "=", "\t"); err != nil {
  39. log.Fatal("json indent: %v", err)
  40. }
  41. fmt.Printf("%s", out.Bytes())
  42.  
  43. var zz = out.Bytes()
  44.  
  45. // will now unmarshal to the struct.
  46. fmt.Printf("========= about to unmarshal out bytes \n\n\n")
  47.  
  48. var m Message
  49.  
  50. fmt.Printf("========= first will print zz \n\n\n")
  51. fmt.Printf("%s", zz)
  52. fmt.Printf("------ about to do the marshal \n\n\n")
  53.  
  54. ******************* this is where is it crashing... what am I doing wrong?
  55. err = json.Unmarshal(zz, &m)
  56. if err != nil {
  57. fmt.Printf("============there was an error... \n\n\n")
  58. log.Fatal(err)
  59. //******** I am getting this error...
  60. // 2016/01/29 14:24:35 invalid character '=' looking for beginning of value
  61. // exit status 1
  62. }
  63.  
  64. }
  65.  
  66. // I would like to turn out.Bytes into some kind of Array or JSON object
  67. // That I can manipulate. This code just Pretty prints the JSON.
  68. // My goal is to create some kind of dictionary or MAP where I can reference
  69. // these records directly.
  70. // but It appears that all I am doing in this code is taking a STRING that just
  71. // happens to be JSON, and using Indent on it. Can someone give me some sugestions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement