英文:
How to Unmarshal the json array?
问题
当我解组json数组时出现了问题。我该如何纠正它?代码如下:
package main
import (
"encoding/json"
"fmt"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Name string
Servers []Server
}
func main() {
var s []Serverslice
str := `{"name":"dxh","servers":[{"serverName":"VPN0","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}],
"name":"dxh1","servers":[{"serverName":"VPN1","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
json.Unmarshal([]byte(str), &s) //有问题的行.....................
fmt.Println(len(s))
}
以下是翻译好的代码部分,你可以直接使用:
package main
import (
"encoding/json"
"fmt"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Name string
Servers []Server
}
func main() {
var s []Serverslice
str := `{"name":"dxh","servers":[{"serverName":"VPN0","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}],
"name":"dxh1","servers":[{"serverName":"VPN1","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
json.Unmarshal([]byte(str), &s) //有问题的行.....................
fmt.Println(len(s))
}
希望对你有帮助!如果还有其他问题,请随时提问。
英文:
There is something wrong when I unmarshal the json array.
How do I correct it ? the code is:http://play.golang.org/p/AtU9q8Hlye
package main
import (
"encoding/json"
"fmt"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Name string
Servers []Server
}
func main() {
var s []Serverslice
str := `{"name":"dxh","servers":[{"serverName":"VPN0","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}],
"name":"dxh1,"servers":[{"serverName":"VPN1","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
json.Unmarshal([]byte(str), &s) //the wrong line.....................
fmt.Println(len(s))
}
答案1
得分: 4
首先,你忽略了json.Unmarshal
的错误返回值。你可能想要像这样做一些修改:
if err := json.Unmarshal([]byte(str), &s); err != nil {
log.Fatalln(err)
}
通过这个修改,我们可以看到你的JSON数据不是有效的:invalid character 's' after object key:value pair
。第二行的"dxh1
末尾缺少引号。
修复这个错误并重新运行程序,你会得到一个不同的错误:json: cannot unmarshal object into Go value of type []main.Serverslice
。这里有两个可能的问题:
-
你想要解码成一个对象。在这种情况下,只需将
s
声明为Serverslice
类型。这是一个修改后的程序版本:点击这里 -
你的JSON应该是一个数组(可能是因为它似乎有重复的键)。这是一个更新后的版本,将JSON更改为提供一个数组:点击这里
英文:
First of all, you're ignoring the error return value from json.Unmarshal
. You probably want something like:
if err := json.Unmarshal([]byte(str), &s); err != nil {
log.Fatalln(err)
}
With that change, we can see that your JSON data isn't valid: invalid character 's' after object key:value pair
. There is a missing quote at the end of "dxh1
on the second line.
Fixing that error and rerunning the program you'll get a different error: json: cannot unmarshal object into Go value of type []main.Serverslice
. There are two possible problems here:
-
You meant to decode into an object. In this case, just declare
s
as aServerslice
. Here is a version of your program that makes that change: http://play.golang.org/p/zgyr_vnn-_ -
Your JSON is supposed to be an array (possible, since it seems to have duplicate keys). Here's an updated version with the JSON changed to provide an array: http://play.golang.org/p/Wl6kUaivEm
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论