使用Golang重构Json

时间:2017-02-14 16:18:33

标签: arrays json go struct

我有以下JSON响应:

[
      {
        "talent_id": "b520ad50-5302-45ce-9121-5ff42d67b4fb",
        "platform": "facebook",
        "posts": [
          {
            "insights": [
              {
                "name": "post_impressions_organic_unique",
                "values": [
                  {
                    "value": 1828
                  }
                ]
              },
              {
                "name": "post_stories_by_action_type",
                "values": [
                  {
                    "like": 42
                  }
                ]
              }
            ],
            "type": "photo",
            "post_id": "24225267232_10154099759037233"
          },
          {
            "insights": [
              {
                "name": "post_impressions_organic_unique",
                "values": [
                  {
                    "value": 864
                  }
                ]
              },
              {
                "name": "post_stories_by_action_type",
                "values": [
                  {
                    "like": 19
                  }
                ]
              }
            ],
            "type": "photo",
            "post_id": "24225267232_10154099756677233"
          }
        ]
      }
    ]

我需要将它重组/压平成这样的东西:

{
    "talent_id": "b520ad50-5302-45ce-9121-5ff42d67b4fb",
    "platform": "facebook",
    "posts": [{
        "post_id": "24225267232_10154051404062233",
        "type": "photo",
        "organic_impressions_unique": 8288,
        "post_story_actions_by_type": {
            "shares": 234,
            "comments": 838,
            "likes": 8768
        }
    }, {
        "post_id": "24225267232_10154051404062233",
        "type": "photo",
        "organic_impressions_unique": 8288,
        "post_story_actions_by_type": {
            "shares": 234,
            "comments": 838,
            "likes": 8768
        }
    }]
}

我使用结构来映射JSON响应:

type JsonData struct {
    TalentID string `json:"talent_id"`
    Platform string `json:"platform"`
    Posts []struct {
        PostID string `json:"post_id"`
        Type string `json:"type"`
        Insights []struct {
            //Data []map[string]interface{}
            Name   string `json:"name"`
        } `json:"insights"`
    } `json:"posts"`
}

我的问题在于帖子中的数据以及如何映射它,我使用地图填充数据并使用Marshal来生成JSON的新结构。

这是我的代码:

    messages := [] JsonData{}
    json.Unmarshal(body, &messages)

    m := make(map[string]interface{})
    m["talent_id"] = messages[0].TalentID
    m["platform"] = messages[0].Platform

    for _, p := range messages[0].Posts {

        for _, i := range p.Insights {
            // here is where I got lost and couldn't know how to fill the data inside the posts
        }
    }


    jsonString, err := json.Marshal(m)
    if err != nil {
        fmt.Println("error:", err)
    }
    fmt.Println(string(jsonString))

P.S。指标post_impressions_organic_unique和post_stories_by_action_type不是静态的,可以更改,也可以在此处返回其他键

1 个答案:

答案 0 :(得分:0)

将您的结构保持为:

type NameValue struct {
    Name string `json:"name"`
    Values []map[string]interface{} `json:"values"`
}

type JsonData struct {
    TalentID string `json:"talent_id"`
    Platform string `json:"platform"`
    Posts []struct {
        PostID string `json:"post_id"`
        Type string `json:"type"`
        Insights []NameValue `json:"insights"`
    } `json:"posts"`
}

创建另一个与此类似的结构:

type JsonDataRes struct {
    TalentID string `json:"talent_id"`
    Platform string `json:"platform"`
    Posts []map[string]interface{} `json:"posts"`
}

JsonData结构中包含数据后,循环访问权限对象并手动为新JsonDataRes对象以及PostId和{{1}的值分配值}帖子内Type