如何解组 JSON 数组?

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

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。这里有两个可能的问题:

  1. 你想要解码成一个对象。在这种情况下,只需将s声明为Serverslice类型。这是一个修改后的程序版本:点击这里

  2. 你的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:

  1. You meant to decode into an object. In this case, just declare s as a Serverslice. Here is a version of your program that makes that change: http://play.golang.org/p/zgyr_vnn-_

  2. 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

huangapple
  • 本文由 发表于 2014年3月16日 09:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/22432011.html
匿名

发表评论

匿名网友

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

确定