douliaodun9153 2017-02-14 16:18
浏览 81

使用Golang重组Json

I have the following JSON response:

[
      {
        "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"
          }
        ]
      }
    ]

I need to restructure/flatten it into something like this:

{
    "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
        }
    }]
}

I am using a struct to map the JSON response:

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"`
}

My problem is with the data inside the posts and how can I map it, I am using a map to fill the data and Marshal it to generate the new structure of JSON.

Here is my code:

    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. the metrics post_impressions_organic_unique and post_stories_by_action_type are not static, they can change or other keys might be returned here

  • 写回答

1条回答 默认 最新

  • dshm8998473 2017-02-15 11:01
    关注

    Keep your struct as:

    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"`
    }
    

    Create another struct similar to this one:

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

    Once you have the data in your JsonData struct, loop over the insights object and manually assign values to the new JsonDataRes object and also the vaules of PostId and Type inside the Posts []map[string]interface{}

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集