drxvjnx58751 2018-09-10 12:47
浏览 64
已采纳

如何通过结构重新格式化JSON?

I have pulled some data out of a given database - the format however is not correct as per what I want.

Sample struct:

type myStruct struct {
    ID                  int    `json:"Id"`
    Language            string `json:"Language"`
    Location            string `json:"Location"`
}

Right, I serialize a given map of strings with a custom struct representing the given returned columns:

func transformJSON(extract []map[string]*sql.SqlCell) ([]byte, error) {
    return json.MarshalIndent(extract, "", " ")
}

This returns valid JSON, but with the format:

  {
      {
      "id": {
       "Value": {
        "Long": 12353
       }
      },
      "language": {
       "Value": {
        "String_": "ja-JP"
       }
      },
      "location": {
       "Value": {
        "String_": "Osaka"
       }
  },

Referring to my struct above, I'd like a format as such:

 [
  {
    "Id": 12353,
    "Language": "ja-JP",
    "Location": "Osaka"
  },
  // .. other objects
 ]

Assuming the same input to the func transformJSON, how would I match the nested keys with the column type values as well?

Should I Marshal the map first, then transform the data -- or work directly with the []map[string]*sql.SqlCell data structure before marshal?

In my head, I was thinking along the lines of:

func transformJSON(extract []map[string]*sql.SqlCell) ([]byte, error) {
    struct :=[] myStruct{}
    // Loop over extract objects, match nested keys, write to struct?

    return json.MarshalIndent(struct, "", " ")
}
  • 写回答

1条回答 默认 最新

  • dongshendi3599 2018-09-10 13:03
    关注

    Create a custom unmarshaler, that unmarshals the entire thing, then converts for you:

    type MyStruct struct {
        ID                  int    `json:"Id"`
        Language            string `json:"Language"`
        Location            string `json:"Location"`
    }
    
    type val struct {
        Value struct {
            String string `json:"String_"`
            Long   int    `json:"Long"`
        }
    }
    
    func (s *MyStruct) UnmarshalJSON(p []byte) error {
        var result struct{
            ID       val `json:"Id"`
            Language val `json:"language"`
            Location val `json:"location"`
        }
        if err := json.Unmarshal(p, &result); err != nil {
            return err
        }
        s.ID = result.ID.Value.Long
        s.Language = result.Langauge.Value.String
        s.Location= result.Location.Value.String
        return nil
    }
    

    Note that minimal error checking and data validation is done in this example--I leave that as an exercise for the reader.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算