Golang,将JSON解码为自定义结构

huangapple go评论90阅读模式
英文:

Golang, Decoding json into custom structure

问题

我正在尝试从 JSON API 中提取 Reddit 内容,并将其转换为客户端的自定义结构。我在 Go 语言中设计的结构如下:

type Subreddit struct {
    offset        int
    num_of_posts  int
    subscribers   int
    thumbnail     string
    children      []post
}

type post struct {
    postType      string
    url           string
    thumbnail     string
    submittedBy   string
    upvotes       int
    downvotes     int
}

不幸的是,Reddit 的 JSON 格式与此相差甚远,而且我还想过滤掉我无法支持的 URL。

我所知道的唯一方法是为源数据中的每个 "children" 创建一个接口,并手动迭代每个子项,为每个接口创建一个单独的 "post",然后将它们推入 subreddit 对象的 post 数组中。

参考数据的格式如下:http://www.reddit.com/r/web_design/.json

这样做是正确的方式吗?还是有更快的方法?对于这样一个小任务来说,似乎需要很多额外的工作,但作为一个 PHP 和 JavaScript 开发者,这对我来说有点不寻常。

英文:

I'm trying to pull reddit content from the json API into a custom structure for the client. the structure i've come up with in go for this is

type Subreddit struct {
        offset int
        num_of_posts int
        subscribers: int
        thumbnail string
        children []post
}

type post struct {
        type string
        url string
        thumbnail string
        submitted_by string
        upvotes int
        downvotes int
}       

unfortunately the reddit json isn't formatted even close to this and in addition i'll want to filter out url's i can't support etc.

The only way i know to do it this is to create an interface for each of the "children" in the source data, and iterate through each child manually, creating an individual "post" for each interface. and pushing them into the subreddit object's post array.

For reference the data is formatted like http://www.reddit.com/r/web_design/.json

Is this the right way to do this? Or is there a faster way. It seems like a lot of overhead for such a small task, but i'm a PHP Javascript dev, so It's just unusual for me I suppose.

答案1

得分: 4

在回答问题之前,我要提醒你,你的结构字段必须是公开的,才能与encoding/json包一起使用。

其次,我必须承认我不太确定你在整个"为每个"子项"创建一个接口"部分的意思是什么。但听起来很复杂😉
无论如何,以下是答案:

如果你希望使用标准的encoding/json包来解组json,除非你使用与Reddit相似的结构,否则你必须使用一个中间结构。

下面是一个示例,展示了如何将Reddit结构的部分映射到Go结构中。通过将json解组为RedditRoot的实例,你可以轻松地遍历Children,删除任何不需要的子项,并填充你的Subreddit结构:

type RedditRoot struct {
    Kind string     `json:"kind"`
    Data RedditData `json:"data"`
}

type RedditData struct {
    Children []RedditDataChild `json:"children"`
}

type RedditDataChild struct {
    Kind string `json:"kind"`
    Data *Post  `json:"data"`
}

type Post struct {
    Type         string `json:"-"`
    Url          string `json:"url"`
    Thumbnail    string `json:"thumbnail"`
    Submitted_by string `json:"author"`
    Upvotes      int    `json:"ups"`
    Downvotes    int    `json:"downs"`
}
英文:

Before I even start to answer the question:
Remember that your struct fields must be exported in order to be used with the encoding/json package.

Secondly I must admit I am not entirely sure what you meant with the entire create an interface for each of the "children" part. But it sounded complicated Golang,将JSON解码为自定义结构
Anyway, to your answer:

If you wish to use the standard encoding/json package to unmarshal the json, you must use an intermediate structure unless you will use a similar structure as the one used by Reddit.

Below you can find an example of how parts of the Reddit structure might be mapped to Go structs. By Unmarshalling the json into an instance of RedditRoot, you can then easily iterate over the Children , remove any unwanted child, and populate your Subreddit struct:

type RedditRoot struct {
	Kind string     `json:"kind"`
	Data RedditData `json:"data"`
}

type RedditData struct {
	Children []RedditDataChild `json:"children"`
}

type RedditDataChild struct {
	Kind string `json:"kind"`
	Data *Post  `json:"data"`
}

type Post struct {
	Type         string `json:"-"` // Is this equal to data.children[].data.kind?
	Url          string `json:"url"`
	Thumbnail    string `json:"thumbnail"`
	Submitted_by string `json:"author"`
	Upvotes      int    `json:"ups"`
	Downvotes    int    `json:"downs"`
}

huangapple
  • 本文由 发表于 2014年1月1日 16:20:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/20866817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定