Go:意外的 JSON 输入结束和 json.Unmarshal 返回空值

huangapple go评论73阅读模式
英文:

Go: unexpected end of JSON input and json.Unmarshal returns nil values

问题

我正在测试如何解析我正在使用的API的JSON响应。

JSON的格式大致如下:

body := []byte(`[
    {"name":"Name1", "value":100.00},
    {"name":"Name2", "value":200.00}
]`)

我已经搜索了各种方法来实现这个,但是无法使其工作。解析返回了零值。我还收到了一个错误消息"unexpected end of JSON input"(在示例中我已经删除了错误处理)。

完整的代码示例可以在这里找到:https://play.golang.org/p/VMdWuAm6HS

参考链接:

  1. https://godoc.org/encoding/json#RawMessage
  2. https://stackoverflow.com/questions/27994327/golang-json-unmarshal-unexpected-end-of-json-input
  3. https://stackoverflow.com/questions/28254102/how-to-unmarshal-json-into-interface-in-golang
英文:

I'm testing out how to unmarshal a json response from an API I'm using.

The json looks something like the following;

body := []byte(`[
	{"name":"Name1", "value":100.00},
	{"name":"Name2", "value":200.00}
]`)

I've searched around for various ways to do this, but fail to get this to work. The unmarshal returns zero values. I also get an error "unexpected end of JSON input" (I've removed the error handling in the example).

Full code example - https://play.golang.org/p/VMdWuAm6HS

Reference:

  1. https://godoc.org/encoding/json#RawMessage
  2. https://stackoverflow.com/questions/27994327/golang-json-unmarshal-unexpected-end-of-json-input
  3. https://stackoverflow.com/questions/28254102/how-to-unmarshal-json-into-interface-in-golang

答案1

得分: 2

你的输入JSON可以使用简单的[]Obj进行建模,其中Obj是你的类型:

type Obj struct {
    Name  string  `json:"name"`
    Value float32 `json:"value"`
}

实际上不需要其他内容:

body := []byte(`[
    {"name":"Name1", "value":100.00},
    {"name":"Name2", "value":200.00}]`)

var res []Obj
err := json.Unmarshal(body, &res)
fmt.Printf("%#v\n%v\n", res, err)

输出包含来自输入JSON的数据(在Go Playground上尝试一下):

[]main.Obj{main.Obj{Name:"Name1", Value:100}, main.Obj{Name:"Name2", Value:200}}
<nil>

回到你的代码:

你的问题在于你使用了这个模型:

type Obj struct {
    Name string `json:"name"`
    Value float32 `json:"value"`
}

type Result struct {
    Data json.RawMessage
}

var res []Result

但是这个res变量将对应以下JSON:

[
    {"Data":{"name":"Name1", "value":100.00}},
    {"Data":{"name":"Name2", "value":200.00}}
]

我想你可以看到区别:这里的数组元素是带有"Data"字段的JSON对象,然后使用你的Obj进行建模。你可以使用你原来的解析代码正确解析这个输入JSON,在Go Playground上尝试一下:

[{{"name":"Name1", "value":100.00}} {{"name":"Name2", "value":200.00}}] 
&main.Obj{Name:"Name1", Value:100} 
&main.Obj{Name:"Name2", Value:200}
英文:

Your input JSON can be modeled with a simple []Obj where Obj is your type:

type Obj struct {
	Name  string  `json:&quot;name&quot;`
	Value float32 `json:&quot;value&quot;`
}

Nothing else is required, really:

body := []byte(`[
	{&quot;name&quot;:&quot;Name1&quot;, &quot;value&quot;:100.00},
	{&quot;name&quot;:&quot;Name2&quot;, &quot;value&quot;:200.00}]`)

var res []Obj
err := json.Unmarshal(body, &amp;res)
fmt.Printf(&quot;%#v\n%v\n&quot;, res, err)

Output contains the data from the input JSON (try it on the Go Playground):

[]main.Obj{main.Obj{Name:&quot;Name1&quot;, Value:100}, main.Obj{Name:&quot;Name2&quot;, Value:200}}
&lt;nil&gt;

Back to your code:

Where you're going wrong is that you use this model:

type Obj struct {
	Name string `json:&quot;name&quot;`
	Value float32 `json:&quot;value&quot;`
}

type Result struct {
	Data json.RawMessage
}

var res []Result

But this res variable would model the following JSON:

[
	{&quot;Data&quot;:{&quot;name&quot;:&quot;Name1&quot;, &quot;value&quot;:100.00}},
	{&quot;Data&quot;:{&quot;name&quot;:&quot;Name2&quot;, &quot;value&quot;:200.00}}
]

I think you can see the difference: the elements of the array here are JSON Objects with a &quot;Data&quot; field, which then are modeled with your Obj. This input JSON is then properly parsed with your original parsing code, you can try it on the Go Playground:

[{{&quot;name&quot;:&quot;Name1&quot;, &quot;value&quot;:100.00}} {{&quot;name&quot;:&quot;Name2&quot;, &quot;value&quot;:200.00}}] 
&amp;main.Obj{Name:&quot;Name1&quot;, Value:100} 
&amp;main.Obj{Name:&quot;Name2&quot;, Value:200}

huangapple
  • 本文由 发表于 2016年12月4日 01:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/40950224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定