dsiy62758 2014-01-01 08:20
浏览 71
已采纳

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

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条回答 默认 最新

  • doudun1934 2014-01-01 13:50
    关注

    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 ;)
    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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里