我正在使用Twitter的api,試圖從中獲取json數(shù)據(jù)http://search.twitter.com/trends/current.json看起來像:{"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\""}]}}我的結構看起來像: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 }然后將JSON解析為type的變量Trending。我是JSON的新手,所以我主要關心的是確保已正確設置數(shù)據(jù)結構以保存返回的json數(shù)據(jù)。我在“ Go”中為學校項目寫了這個。(這不是特定任務的一部分,而只是我正在演示有關該語言的演示內(nèi)容)更新:根據(jù)PeterSO的評論,我要使用正則表達式。使用:Cur_Trends := new(Current)/* unmarshal the JSON into our structures *///find proper json time-nameaoUnixTime, _, _ := 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)不顯示任何更改。我指定的regexp錯誤嗎?看起來“開始”一次只允許一個正則表達式...更新:現(xiàn)在可以將日期/時間更改為“ ntrends”,但“取消編組”無效。我可以使用json.Decode將所有內(nèi)容移入接口,但無法對其進行迭代...使用“ for ... range”給我奇怪的東西...
- 3 回答
- 0 關注
- 298 瀏覽
添加回答
舉報
0/150
提交
取消