duannaxin9975
2015-10-10 05:05
浏览 369
已采纳

如何在Go中将JSON对象数组转换为具有默认值的结构数组?

I'm working on a Go API that can receive POSTs consisting of a JSON array of objects. The structure of the POST will look something like:

[
  {
    "name":"Las Vegas",
    "size":14
  },
  {
    "valid": false,
    "name":"Buffalo",
    "size":63
  }
]  

Let's say I have the following struct:

type Data {
    Valid    bool
    Name     string
    Size     float64
}

I want to create a bunch of Datas with Valid set to true anytime it's not actually specified in the JSON as false. If I were doing a single one I could use How to specify default values when parsing JSON in Go, but for doing an unknown number of them the only thing I've been able to come up with is something like:

var allMap []map[string]interface{}
var structs []Data
for _, item := range allMap {
  var data Data
  var v interface{}
  var ok bool
  if v, ok := item["value"]; ok {
    data.Valid = v
  } else {
    data.Valid = true
  }
  id v, ok := item["name"]; ok {
    data.Name = v
  }
  ...
  structs = append(structs, data)
}
return structs

Right now the struct I'm actually working with has 14 fields, some of them have values I want to assign defaults, others are fine to leave blank, but all of them have to be iterated through using this approach.

Is there a better way?

图片转代码服务由CSDN问答提供 功能建议

我正在使用Go API,该API可以接收由JSON对象数组组成的POST。 POST的结构类似于:

  [
 {
“ name”:“ Las Vegas”,
“ size”:14 
},  
 {
“ valid”:否,
“ name”:“ Buffalo”,
“ size”:63 
} 
] 
   
 
 

假设我具有以下结构:

  type数据{
有效bool 
名称字符串
大小float64 
} 
   
 
 

我想创建一堆 Data ,并且将 Valid 设置为 true 的任何时候都未实际指定 JSON为 false 。 如果我只做一个,可以使用如何 在Go中解析JSON时指定默认值,但是对于执行未知数量的操作,我唯一能想到的就是这样的东西:

  var allMap [] map [string] interface {} 
var structs [] Data 
for _,item:= range allMap {
 var data Data 
 var v interface {} 
 var ok bool 
如果v, 好的:= item [“ value”];  ok {
 data.Valid = v 
} else {
 data.Valid = true 
} 
 id v,ok:= item [“ name”]; 好的{
 data.Name = v 
} 
 ... 
 structs = append(结构,数据)
} 
返回结构
   
 
 

现在,我实际使用的结构具有14个字段,其中一些具有我要分配默认值的值,另一些可以保留空白,但是所有这些都必须使用此方法进行迭代。

有更好的方法吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyue0263 2015-10-10 05:29
    已采纳

    You can use the json.RawMessage type to defer unmarshaling some JSON text value. If you use this type, then the JSON text will be stored in this without unmarshaling (so you can unmarshal this fragment later on as you wish).

    So in your case if you try to unmarshal into a slice of such RawMessage, you can use the technique what you linked in your question, that is you can iterate over the slice of raw values (which are the JSON text for each Data), create a Data struct with values you want as defaults for missing values, and unmarshal a slice element into this prepared struct. That's all.

    It looks like this:

    allJson := []json.RawMessage{}
    if err := json.Unmarshal(src, &allJson); err != nil {
        panic(err)
    }
    
    allData := make([]Data, len(allJson))
    for i, v := range allJson {
        // Here create your Data with default values
        allData[i] = Data{Valid: true}
        if err := json.Unmarshal(v, &allData[i]); err != nil {
            panic(err)
        }
    }
    

    Try it on the Go Playground.

    Notes / Variants

    For efficiency (to avoid copying structs), you can also make the allData to be a slice of pointers in the above example, which would look like this:

    allData := make([]*Data, len(allJson))
    for i, v := range allJson {
        // Here create your Data with default values
        allData[i] = &Data{Valid: true}
        if err := json.Unmarshal(v, allData[i]); err != nil {
            panic(err)
        }
    }
    

    If you want to keep using non-pointers, for efficiency you can "prepare" your wished default values in the slice elements itself, which would look like this:

    allData := make([]Data, len(allJson))
    for i, v := range allJson {
        // Here set your default values in the slice elements
        // Only set those which defer from the zero values:
        allData[i].Valid = true
        if err := json.Unmarshal(v, &allData[i]); err != nil {
            panic(err)
        }
    }
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题