我正在使用Twitter的api,试图从中获取json数据
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的新手,所以我主要关心的是确保已正确设置数据结构以保存返回的json数据。
我在“ Go”中为学校项目写了这个。(这不是特定任务的一部分,而只是我正在演示有关该语言的演示内容)
更新:根据PeterSO的评论,我要使用正则表达式。使用:
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)
不显示任何更改。我指定的regexp错误吗?看起来“开始”一次只允许一个正则表达式...
更新:现在可以将日期/时间更改为“ ntrends”,但“取消编组”无效。我可以使用json.Decode将所有内容移入接口,但无法对其进行迭代...
使用“ for ... range”给我奇怪的东西...
相关分类