为了账号安全,请及时绑定邮箱和手机立即绑定

基本的json>结构问题(使用'Go')

基本的json>结构问题(使用'Go')

Go
一只斗牛犬 2021-04-02 10:09:04
我正在使用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-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错误吗?看起来“开始”一次只允许一个正则表达式...更新:现在可以将日期/时间更改为“ ntrends”,但“取消编组”无效。我可以使用json.Decode将所有内容移入接口,但无法对其进行迭代...使用“ for ... range”给我奇怪的东西...
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 263 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信