Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I am getting an error around the unmarshal statement...
- // can someone give me a hint please...
- // I am just a newbie...
- package main
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- )
- const (
- url01 = "http://testrest01.trainscheds.com/api/v2/VYTEST02-B.json"
- url02 = "http://ts04.trainscheds.com/api/v4/rest_brun_rates_json"
- )
- type Message struct {
- TIMETEXT string
- Body string
- title string
- }
- func main() {
- // 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"}]`
- res, err := http.Get(url01)
- //res, err := http.Get(url02)
- if err != nil {
- log.Fatal(err)
- }
- input, err := ioutil.ReadAll(res.Body)
- var out bytes.Buffer
- if err := json.Indent(&out, []byte(input), "=", "\t"); err != nil {
- log.Fatal("json indent: %v", err)
- }
- fmt.Printf("%s", out.Bytes())
- var zz = out.Bytes()
- // will now unmarshal to the struct.
- fmt.Printf("========= about to unmarshal out bytes \n\n\n")
- var m Message
- fmt.Printf("========= first will print zz \n\n\n")
- fmt.Printf("%s", zz)
- fmt.Printf("------ about to do the marshal \n\n\n")
- ******************* this is where is it crashing... what am I doing wrong?
- err = json.Unmarshal(zz, &m)
- if err != nil {
- fmt.Printf("============there was an error... \n\n\n")
- log.Fatal(err)
- //******** I am getting this error...
- // 2016/01/29 14:24:35 invalid character '=' looking for beginning of value
- // exit status 1
- }
- }
- // I would like to turn out.Bytes into some kind of Array or JSON object
- // That I can manipulate. This code just Pretty prints the JSON.
- // My goal is to create some kind of dictionary or MAP where I can reference
- // these records directly.
- // but It appears that all I am doing in this code is taking a STRING that just
- // 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