简体   繁体   English

Golang将JSON映射为结构

[英]Golang map json to struct

I have a JSON which I need to extract the data out of it using a struct: 我有一个JSON,我需要使用struct从其中提取数据:

I am trying to map it to the below struct: 我正在尝试将其映射到以下结构:

type Message struct {
    Name   string `json:"name"`
    Values []struct {
        Value int `json:"value,omitempty"`
        Comments int `json:"comments,omitempty"`
        Likes    int `json:"likes,omitempty"`
        Shares   int `json:"shares,omitempty"`
    } `json:"values"`
}

This is my json: 这是我的json:

[{
        "name": "organic_impressions_unique",
        "values": [{
            "value": 8288
        }]
    }, {
        "name": "post_story_actions_by_type",
        "values": [{
            "shares": 234,
            "comments": 838,
            "likes": 8768
        }]
    }]

My questions are: 我的问题是:

  1. How to structure my struct? 如何构造我的结构?
  2. How to read the name, values and comments? 如何读取名称,值和注释?

So far I couldn't read the data using the below code: 到目前为止,我无法使用以下代码读取数据:

msg := []Message{}
getJson("https://json.url", msg)
println(msg[0])

the getJson function: getJson函数:

func getJson(url string, target interface{}) error {
    r, err := myClient.Get(url)
    if err != nil {
        return err
    }
    defer r.Body.Close()

    return json.NewDecoder(r.Body).Decode(target)
}

Your struct is correct. 您的结构是正确的。 All you need is love to use json.Unmarshal function with a correct target object which is slice of Message instances: []Message{} 您需要做的就是使用json.Unmarshal函数以及正确的目标对象,该对象是Message实例的一部分: []Message{}

Correct unmarshaling : 正确拆封

type Message struct {
    Name   string `json:"name"`
    Values []struct {
        Value    int `json:"value,omitempty"`
        Comments int `json:"comments,omitempty"`
        Likes    int `json:"likes,omitempty"`
        Shares   int `json:"shares,omitempty"`
    } `json:"values"`
}

func main() {
    input := []byte(`
[{
    "name": "organic_impressions_unique",
    "values": [{
        "value": 8288
    }]
    }, {
        "name": "post_story_actions_by_type",
        "values": [{
            "shares": 234,
            "comments": 838,
            "likes": 8768
        }]
    }]
`)

    messages := []Message{} // Slice of Message instances
    json.Unmarshal(input, &messages)
    fmt.Println(messages)
}

Your JSON seems to be an array. 您的JSON似乎是一个数组。 Just unmarshall it to a slice. 只需将其解组即可。 Something like: 就像是:

var messages []Message
err := json.Unmarshal(json, &messages)

Should work. 应该管用。

I don't know if this will be any help now, but i've recently written utility for generating exact go type from json input: https://github.com/m-zajac/json2go 我不知道现在是否会有帮助,但是我最近编写了用于从json输入生成确切的go类型的实用程序: https : //github.com/m-zajac/json2go

For json from first post it generates this struct: 对于第一篇文章中的json,它将生成以下结构:

type Object struct {
    Name    string  `json:"name"`
    Values  []struct {
        Comments    *int    `json:"comments,omitempty"`
        Likes       *int    `json:"likes,omitempty"`
        Shares      *int    `json:"shares,omitempty"`
        Value       *int    `json:"value,omitempty"`
    }   `json:"values"`
}

You can decode data to this struct like this: 您可以像这样将数据解码到该结构:

var docs []Object
if err := json.Unmarshal(input, &docs); err != nil {
    // handle error
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM