dongxiezhi0590
2015-05-25 20:18
浏览 3.3k
已采纳

在Go中解析JSON时如何指定默认值

I want to parse a JSON object in Go, but want to specify default values for fields that are not given. For example, I have the struct type:

type Test struct {
    A string
    B string
    C string
}

The default values for A, B, and C, are "a", "b", and "c" respectively. This means that when I parse the json:

{"A": "1", "C": 3}

I want to get the struct:

Test{A: "1", B: "b", C: "3"}

Is this possible using the built-in package encoding/json? Otherwise, is there any Go library that has this functionality?

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

我想在Go中解析JSON对象,但想为未给出的字段指定默认值。 例如,我具有结构类型:</ p>

  type测试结构{
 A字符串
 B字符串
 C字符串
} 
 </ code> </  pre> 
 
 

A,B和C的默认值分别为“ a”,“ b”和“ c”。 这意味着当我解析json时:</ p>

  {“ A”:“ 1”,“ C”:3} 
 </ code> </ pre> 
  
 

我想获取结构:</ p>

  Test {A:“ 1”,B:“ b”,C:“ 3”} 
 </  code> </ pre> 
 
 

使用内置包 encoding / json </ code>是否可以? 否则,是否有任何具有此功能的Go库?</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题