- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试使用他们的开放 API 获取特定 Subreddit 的热门提交列表:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
const api_endpoint = "http://www.reddit.com/r/%s/top.json?t=all&limit=100"
func main() {
// Get console argument 'subreddit' and format the API endpoint URL.
subreddit := os.Args[1]
top_by_subreddit_endpoint := fmt.Sprintf(api_endpoint, subreddit)
// Hit the API service.
response, err := http.Get(top_by_subreddit_endpoint)
if err != nil {
//Perror(err)
} else {
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
//Perror(err)
} else {
var top_submissions Submission
json.Unmarshal(content, &top_submissions)
}
}
}
type ByCreated []Submission
func (a ByCreated) Len() int { return len(a) }
func (a ByCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCreated) Less(i, j int) bool { return a[i].Created < a[j].Created }
type Submission struct {
Domain string `json:"domain"`
BannedBy string `json:"banned_by"`
MediaEmbed string `json:"media_embed"`
Subreddit string `json:"subreddit"`
SelftextHtml string `json:"selftext_html"`
Selftext string `json:"selftext"`
Likes bool `json:"likes"`
SecureMedia string `json:"secure_media"`
LinkFlairText string `json:"link_flair_text"`
Id string `json:"id"`
Gilded int `json:"gilded"`
SecureMediaEmbed string `json:"secure_media_embed"`
Clicked bool `json:"clicked"`
Stickied bool `json:"stickied"`
Author string `json:"author"`
Media string `json:"media"`
Score int `json:"score"`
ApprovedBy string `json:"approved_by"`
Over18 bool `json:"over_18"`
Hidden bool `json:"hidden"`
Thumbnail string `json:"thumbnail"`
SubredditId string `json:"subreddit_id"`
Edited int `json:"edited"`
LinkFlairCssClass string `json:"link_flair_css_class"`
AuthorFlairCssClass string `json:"author_flair_css_class"`
Downs int `json:"downs"`
Saved bool `json:"saved"`
IsSelf bool `json:"is_self"`
Permalink string `json:"permalink"`
Name string `json:"name"`
Created int `json:"created"`
Url string `json:"url"`
AuthorFlairText string `json:"author_flair_text"`
Title string `json:"title"`
CreatedUtc int `json:"created_utc"`
Ups int `json:"ups"`
NumComments int `json:"num_comments"`
Visited bool `json:"visited"`
NumReports string `json:"num_reports"`
Distinguished string `json:"distinguished"`
}
不幸的是,这不起作用,因为 actual submission list is nested inside the response .如何将数据 json 数组(位于 children
元素内)解码为 Submission
结构类型的数组?
{
"kind": "Listing",
"data": {
"modhash": "1wcy5kngm9408522707f8e319ca825af342a5b3c460ca7c928",
"children": [
{
"kind": "t3",
"data": {
"domain": "self.Smite",
"banned_by": null,
"media_embed": {},
"subreddit": "Smite",
"selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>Hello Smite community - how&#39;s it going? My name is Steve, but you can just call me Proto. I&#39;m a forum moderator and content creator for Beyond Entertainment - a site which specializes in gaming news surrounding competitive console gaming and all around video games news. I&#39;ve been around competitive gaming since 2007 back in the Halo 2 MLG days and have been hooked ever since. I&#39;m not here to talk about myself though - and although I&#39;m not a Smite player - I&#39;m here to talk about a company who is becoming involved with the Smite community that goes by the name Bonafide Boarding <a href=\"https://twitter.com/LiveBNFD\">https://twitter.com/LiveBNFD</a> </p>\n\n<p>Now you might be asking yourself why am I writing this? First, these 2 tweets drew my attention in regards to Bonafide Boarding <a href=\"https://twitter.com/BonafideBCC\">https://twitter.com/BonafideBCC</a> - as well as Billy and Brad - working with people in the competitive Smite community </p>\n\n<p><a href=\"https://twitter.com/LiveBNFD/status/441044741699149825\">https://twitter.com/LiveBNFD/status/441044741699149825</a> </p>\n\n<p><a href=\"https://twitter.com/HiRezNabil\">https://twitter.com/HiRezNabil</a> (pictures with Brad under this Twitter user&#39;s photos - Brad confirmed in the AGL Facebook cover photo as well just for verification) </p>\n\n<p>Well - I&#39;m writing this because the owners of the Bonafide Boarding Clothing company, Brad Weir and Billy Lutz <a href=\"https://twitter.com/MrBillyLutz\">https://twitter.com/MrBillyLutz</a> have past experience in competitive gaming. including running their own league of Halo tournaments in 2012 and 2013. However, it&#39;s not a good past - and to put it as blunt as possible - the two together are frauds who stole money from the Halo community and owe professional Halo players thousands of dollars from their Halo events - at the time known as AGL (Arena Gaming League) <a href=\"https://www.facebook.com/ArenaGamingLeague\">https://www.facebook.com/ArenaGamingLeague</a> (Brad Weir is in the cover photo, on the right, in the red t shirt interviewing the person on the left) </p>\n\n<p>Brad and Billy had their first AGL Halo event in August of 2012 - and in 2013 is when the problems began. While there is a lot more to talk about then what can be posted here, I will basically go into how AGL ended and how Halo players were scammed out of thousands of dollars in owed prize money - and to this day, no one has received any money from AGL, Brad or Billy for most of the 2013 AGL Halo tournaments. </p>\n\n<p>AGL had announced an event to held in Indianapolis, Indiana on November 1-3, 2013 called the &quot;AGL 10K&quot;. It was made out to be their biggest event yet - held at a brand new LAN center and promising the most prize money at any AGL event thus far at the time. However, just days before the event was scheduled to take place, it was cancelled as noted here with a direct quote from AGL/Brad in the article below </p>\n\n<p><a href=\"http://teambeyond.net/agl-10k-cancelled/\">http://teambeyond.net/agl-10k-cancelled/</a> </p>\n\n<p>This also led to players not being able to get refunds for their team passes which many had already purchased for the event (note the dates on the tweets shown below) </p>\n\n<p><a href=\"https://twitter.com/jCreelz/status/394116351402737664\">https://twitter.com/jCreelz/status/394116351402737664</a> </p>\n\n<p><a href=\"https://twitter.com/Ninja_Invictus/status/393922514247643136\">https://twitter.com/Ninja_Invictus/status/393922514247643136</a> </p>\n\n<p>Not long after this event was cancelled, AGL completely shut down, seemingly out of nowhere - confirmed in this forum post by Brad himself on the Beyond Entertainment forums </p>\n\n<p><a href=\"http://teambeyond.net/forum/index.php?/topic/2238-agl-closes-down-statement/page-50?p=108159#entry108159\">http://teambeyond.net/forum/index.php?/topic/2238-agl-closes-down-statement/page-50?p=108159#entry108159</a> </p>\n\n<p>Since AGL has shut down, there has been little to absolutely no contact from Brad, Billy, or anyone associated with AGL in terms of paying not only the professional players who are owed prize money, but commentators and staff from the event as well who were not paid and had no travel costs covered. </p>\n\n<p>The following are all tweets from professional Halo players who have still NEVER been paid by Brad Weir, Billy Lutz, or AGL as a whole. </p>\n\n<p><a href=\"https://twitter.com/MLGACE/statuses/408356773037232128\">https://twitter.com/MLGACE/statuses/408356773037232128</a> </p>\n\n<p><a href=\"https://twitter.com/FormaL_tK/statuses/408400562372108288\">https://twitter.com/FormaL_tK/statuses/408400562372108288</a> </p>\n\n<p><a href=\"https://twitter.com/Naded_MLG/statuses/426908120879607808\">https://twitter.com/Naded_MLG/statuses/426908120879607808</a> </p>\n\n<p><a href=\"https://twitter.com/xPROVERBx/statuses/426975901583425536\">https://twitter.com/xPROVERBx/statuses/426975901583425536</a> </p>\n\n<p><a href=\"https://twitter.com/enable_/status/426952260317220864\">https://twitter.com/enable_/status/426952260317220864</a> </p>\n\n<p><a href=\"https://twitter.com/BRIZZ_Legit/status/426959849729974272\">https://twitter.com/BRIZZ_Legit/status/426959849729974272</a> </p>\n\n<p>This recent tweet is also from someone who bought t shirts from Bonafide Boarding, Brad and Billy&#39;s clothing company, and it appears they were screwed as well. </p>\n\n<p><a href=\"https://twitter.com/shellyteachesk2/status/447432597288017920\">https://twitter.com/shellyteachesk2/status/447432597288017920</a> </p>\n\n<p>The reason I am writing all this is just to warn you guys as a community to not fall into the trap the Halo community fell into in 2013. Do not support these guys - and most importantly - do not give them your money. I have loved competitive gaming for a long time, and I want nothing more than to see it continue to grow. The last thing I want to see happen is what happened to the Halo community in 2013 at the hands of Brad Weir, Billy Lutz, and the AGL company as a whole. Brad, Billy, and Bonafide Boarding as a whole is nothing but a scam and these 2 owe a lot of people a lot of money. Hopefully you guys can take this to heart and not support these two scumbags who have still yet to pay thousands of dollars to people who rightfully won money in their tournaments. Thank you for reading. </p>\n\n<ul>\n<li>Proto</li>\n</ul>\n</div><!-- SC_ON -->",
"selftext": "Hello Smite community - how's it going? My name is Steve, but you can just call me Proto. I'm a forum moderator and content creator for Beyond Entertainment - a site which specializes in gaming news surrounding competitive console gaming and all around video games news. I've been around competitive gaming since 2007 back in the Halo 2 MLG days and have been hooked ever since. I'm not here to talk about myself though - and although I'm not a Smite player - I'm here to talk about a company who is becoming involved with the Smite community that goes by the name Bonafide Boarding https://twitter.com/LiveBNFD \n\nNow you might be asking yourself why am I writing this? First, these 2 tweets drew my attention in regards to Bonafide Boarding https://twitter.com/BonafideBCC - as well as Billy and Brad - working with people in the competitive Smite community \n\nhttps://twitter.com/LiveBNFD/status/441044741699149825 \n\nhttps://twitter.com/HiRezNabil (pictures with Brad under this Twitter user's photos - Brad confirmed in the AGL Facebook cover photo as well just for verification) \n\nWell - I'm writing this because the owners of the Bonafide Boarding Clothing company, Brad Weir and Billy Lutz https://twitter.com/MrBillyLutz have past experience in competitive gaming. including running their own league of Halo tournaments in 2012 and 2013. However, it's not a good past - and to put it as blunt as possible - the two together are frauds who stole money from the Halo community and owe professional Halo players thousands of dollars from their Halo events - at the time known as AGL (Arena Gaming League) https://www.facebook.com/ArenaGamingLeague (Brad Weir is in the cover photo, on the right, in the red t shirt interviewing the person on the left) \n\nBrad and Billy had their first AGL Halo event in August of 2012 - and in 2013 is when the problems began. While there is a lot more to talk about then what can be posted here, I will basically go into how AGL ended and how Halo players were scammed out of thousands of dollars in owed prize money - and to this day, no one has received any money from AGL, Brad or Billy for most of the 2013 AGL Halo tournaments. \n\nAGL had announced an event to held in Indianapolis, Indiana on November 1-3, 2013 called the \"AGL 10K\". It was made out to be their biggest event yet - held at a brand new LAN center and promising the most prize money at any AGL event thus far at the time. However, just days before the event was scheduled to take place, it was cancelled as noted here with a direct quote from AGL/Brad in the article below \n\nhttp://teambeyond.net/agl-10k-cancelled/ \n\nThis also led to players not being able to get refunds for their team passes which many had already purchased for the event (note the dates on the tweets shown below) \n\nhttps://twitter.com/jCreelz/status/394116351402737664 \n\nhttps://twitter.com/Ninja_Invictus/status/393922514247643136 \n\nNot long after this event was cancelled, AGL completely shut down, seemingly out of nowhere - confirmed in this forum post by Brad himself on the Beyond Entertainment forums \n\nhttp://teambeyond.net/forum/index.php?/topic/2238-agl-closes-down-statement/page-50?p=108159#entry108159 \n\nSince AGL has shut down, there has been little to absolutely no contact from Brad, Billy, or anyone associated with AGL in terms of paying not only the professional players who are owed prize money, but commentators and staff from the event as well who were not paid and had no travel costs covered. \n\nThe following are all tweets from professional Halo players who have still NEVER been paid by Brad Weir, Billy Lutz, or AGL as a whole. \n\nhttps://twitter.com/MLGACE/statuses/408356773037232128 \n\nhttps://twitter.com/FormaL_tK/statuses/408400562372108288 \n\nhttps://twitter.com/Naded_MLG/statuses/426908120879607808 \n\nhttps://twitter.com/xPROVERBx/statuses/426975901583425536 \n\nhttps://twitter.com/enable_/status/426952260317220864 \n\nhttps://twitter.com/BRIZZ_Legit/status/426959849729974272 \n\nThis recent tweet is also from someone who bought t shirts from Bonafide Boarding, Brad and Billy's clothing company, and it appears they were screwed as well. \n\nhttps://twitter.com/shellyteachesk2/status/447432597288017920 \n\nThe reason I am writing all this is just to warn you guys as a community to not fall into the trap the Halo community fell into in 2013. Do not support these guys - and most importantly - do not give them your money. I have loved competitive gaming for a long time, and I want nothing more than to see it continue to grow. The last thing I want to see happen is what happened to the Halo community in 2013 at the hands of Brad Weir, Billy Lutz, and the AGL company as a whole. Brad, Billy, and Bonafide Boarding as a whole is nothing but a scam and these 2 owe a lot of people a lot of money. Hopefully you guys can take this to heart and not support these two scumbags who have still yet to pay thousands of dollars to people who rightfully won money in their tournaments. Thank you for reading. \n\n- Proto",
"likes": true,
"secure_media": null,
"link_flair_text": null,
"id": "21dp8c",
"gilded": 0,
"secure_media_embed": {},
"clicked": false,
"stickied": false,
"author": "Proto__",
"media": null,
"score": 1038,
"approved_by": null,
"over_18": false,
"hidden": false,
"thumbnail": "",
"subreddit_id": "t5_2stl8",
"edited": 1395865323,
"link_flair_css_class": null,
"author_flair_css_class": null,
"downs": 274,
"saved": false,
"is_self": true,
"permalink": "/r/Smite/comments/21dp8c/a_warning_to_the_smite_community_bonafide/",
"name": "t3_21dp8c",
"created": 1395828838,
"url": "http://www.reddit.com/r/Smite/comments/21dp8c/a_warning_to_the_smite_community_bonafide/",
"author_flair_text": null,
"title": "A warning to the Smite community (Bonafide Boarding Clothing company)",
"created_utc": 1395800038,
"ups": 1312,
"num_comments": 200,
"visited": false,
"num_reports": null,
"distinguished": null
}
}],
}
}
}
最佳答案
再创建一些结构(就像您的 Submission
结构)来模仿 JSON 的确切形状。解码到最顶层结构之一,然后访问该结构的适当成员。
关于json - 使用 unmarshal 从响应中获取特定的 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317191/
json: 无法将数组解码为 Go 类型的值 配置 json: { "monitor_servers_info":[ { "server
我有一个看起来像这样的 JSON blob { "metadata":{ "id":"2377f625-619b-4e20-90af-9a6cbfb80040",
我正在向第 3 方 API 发出 JSON 请求。如果我的身份验证 token 已过期,我会收到 {"error": "message"} .如果一切顺利,我会收到有效的回复。 现在,我调用json.
这个问题在这里已经有了答案: json.Marshal(struct) returns "{}" (3 个答案) 关闭 6 年前。 我在解码从 .json 文件中读取的 json 数据时遇到问题 t
我正在尝试从文件中读取并将其加载到结构 slice 中。如 block 注释中所示,我读入的行已正确加载。 我遇到的问题是 class 变量总是返回空值。我做错了什么? func loadClasse
我很难理解为什么下面使用 unmarshal 方法的代码不起作用,但我用 NewDecoder 编写的代码几乎相同,而且运行良好。 package conf import ( "os"
我目前遇到以下问题: 我通过 websocket 得到一个 []byte/string 看起来像 eventname {"JSON": "data", "In": "different formats
我正在从本地主机读取一个 json 文档并尝试将其转换为 Test 类型: type Test struct { one string two string three str
这个问题在这里已经有了答案: My structures are not marshalling into json [duplicate] (3 个答案) 关闭 6 年前。 我有以下代码: pac
我正在开发一个 RSS 阅读器应用程序,遇到了纽约时报 RSS 提要的问题。我已将问题缩小到以下 XML(省略了不必要的字段): https://www.nytimes.com/2017/09/
我有一个如下所示的 JSON 对象: {"API version":"1.2.3"} 我想使用 json.Unmarshal() 将它转换为一个对象去功能。根据this blog post : How
我有一个像这样的 JSON blob { "metadata":{ "id":"2377f625-619b-4e20-90af-9a6cbfb80040", "
我已经被这个问题困扰了几个星期了。我有一个从 Autonomy IDOL 搜索中收到的 XML 文档,可以将其成功解码为一组 Java 对象。但是,如果有国际字符,例如中文、日文、俄文/西里尔文,它们
我有一个图像数据结构 type ImageData struct { Name string Data []byte } 数据字段是转换为字节的图像。 我有jsonImages和[{"
这不是Stop json.Marshal() from stripping trailing zero from floating point number的副本,因为我希望和编码(marshal)(
在此链接:JAXB Unmarshalling XML string - Looping through all tags有人提出了一个很好的解决方案。我试图从中受益,但我无法让它在我的情况下发挥作用
我正在尝试解码一个没有注释的编码对象。 这是我的对象类。 class Student { private String name; private int age; publi
我在各种堆栈溢出帖子和博客条目中看到了以下语法: JAXBElement sc = unmarshaller.unmarshal(is, SomeClass.class); 那么当我尝试使用此语法时,
我必须在 Java 对象中解析 XML,并且 XML 包含一个未包装的对象列表,如下所示: Main Property A Main Property B
刚开始使用 Go,我对我正在学习的教程有一点疑问。我读到 Unmarshall 是某种 JSON 编码,我的疑问是:err = json.Unmarshal(body, &p) 为什么我们要将编码后的
我是一名优秀的程序员,十分优秀!