Note that there are some explanatory texts on larger screens.

plurals
  1. PObasic json > struct question ( using 'Go')
    text
    copied!<p>I'm working with twitter's api, trying to get the json data from</p> <pre><code>http://search.twitter.com/trends/current.json </code></pre> <p>which looks like:</p> <pre><code>{"as_of":1268069036,"trends":{"2010-03-08 17:23:56":[{"name":"Happy Women's Day","query":"\"Happy Women's Day\" OR \"Women's Day\""},{"name":"#MusicMonday","query":"#MusicMonday"},{"name":"#MM","query":"#MM"},{"name":"Oscars","query":"Oscars OR #oscars"},{"name":"#nooffense","query":"#nooffense"},{"name":"Hurt Locker","query":"\"Hurt Locker\""},{"name":"Justin Bieber","query":"\"Justin Bieber\""},{"name":"Cmon","query":"Cmon"},{"name":"My World 2","query":"\"My World 2\""},{"name":"Sandra Bullock","query":"\"Sandra Bullock\""}]}} </code></pre> <p>My structs look like: </p> <pre><code>type trend struct { name string query string } type trends struct { id string arr_of_trends []trend } type Trending struct { as_of string trends_obj trends } </code></pre> <p>and then I parse the JSON into a variable of type <code>Trending</code>. I'm very new to JSON so my main concern is making sure I've have the data structure correctly setup to hold the returned json data.</p> <p>I'm writing this in 'Go' for a project for school. (This is not part of a particular assignment, just something I'm demo-ing for a presentation on the language)</p> <p>UPDATE: In accordance with PeterSO's comment I'm going the regexp route. Using:</p> <pre><code>Cur_Trends := new(Current) /* unmarshal the JSON into our structures */ //find proper json time-name aoUnixTime, _, _ := os.Time() // insert code to find and convert as_of Unix time to aoUnixTime aoName := time.SecondsToUTC(aoUnixTime).Format(`"2006-01-02"`) fmt.Printf("%s\n", aoName) regexp_pattern := "/" + aoName + "/" regex, _ := regexp.Compile(regexp_pattern); cleaned_json := regex.ReplaceAllString(string(body2), "ntrends") os.Stdout.WriteString(cleaned_json) </code></pre> <p>Doesn't show any changes. Am I specifying the regexp wrong? It seems like 'Go' only allows for one regexp at a time...</p> <p>UPDATE: can now change the date/time to "ntrends" but the "Unmarshaling" isn't working. I can get everything moved into an interface using json.Decode, but can't iterate through it...</p> <p>I guess my new question is, How do I iterate through: </p> <pre><code>map[as_of:1.268176902e+09 trends:map[ntrends:[map[name:#nowplaying query:#nowplaying] map[name:#imtiredofseeing query:#imtiredofseeing] map[name:#iWillNever query:#iWillNever] map[name:#inmyfamily query:#inmyfamily] map[name:#raiseyourhandif query:#raiseyourhandif] map[name:#ripbig query:#ripbig] map[name:QVC query:QVC] map[name:#nooffense query:#nooffense] map[name:#RIPLaylaGrace query:#RIPLaylaGrace] map[name:Justin Bieber query:"Justin Bieber"]]]] </code></pre> <p>using "for...range" is giving me weird stuff...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload