让我们看一下以下代码片段:
type Input struct {
Value1 string
Value2 string
Value3 string
Value4 string
Nest
}
type Nest struct {
ID string
}
input := &Input{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&input); err != nil {
fmt.Printf("something went wrong %v", err)
}
fmt.Printf("Json Input = %+v\n", input)
我通过cURL发送以下内容:
curl -k -vvv -X POST -d '{"value1":"test", "value2":"Somevalue", "value3":"othervalue", "Nest":{"ID": "12345"}}' http://localhost:8000/endpoint
..并获得以下输出:
{Value1:test Value2:Somevalue Value3:othervalue Value4: Nest:{ID:}}
问题:
由于某种原因,我无法很好地解码嵌套结构。而且,我不确定这是我的代码还是我的调用方式。
Nest
被嵌入在Input
。
JSON {"value1":"test", "value2":"Somevalue", "value3":"othervalue", "ID": "12345"}
将正确编组到您的中Input
。
如果要使用Question中的JSON正文,则必须更改Input
为以下内容
type Input struct {
Value1 string
Value2 string
Value3 string
Value4 string
Nest Nest
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句