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

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题