Golang的Json没有返回预期的值。

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

Golang Json Not returning expected values

问题

我有一个代码 @ http://play.golang.org/p/HDlJJ54YqW

我想要打印一个人的电话和电子邮件。
它可以有多个条目。
但是出现了未定义的错误。
有人可以帮忙吗?

谢谢。

英文:

I have a code @ http://play.golang.org/p/HDlJJ54YqW

I wanted to print the Phone and email of a person.
It can be of multiple entries.
But getting the error undefined.
Can anyone help out.

Thanks.

答案1

得分: 0

小细节:你引用了两次:你将对象的地址的地址传递给了json.Unmarshal。只需传递地址即可。

`允许多行,不需要拆分你的json输入。

我不知道你试图通过u.Details[Phone:"1111"].Email来实现什么,但这不是Go语法。你的Details成员是Detail的一个切片。切片类似于数组,可以通过索引访问。

此外,你的json与对象结构不匹配。如果你想在一个内容中有多个细节,那么它需要嵌入在一个数组中([ ])。

你可以像这样做:(http://play.golang.org/p/OP1zbPW_wk)

package main

import (
	"encoding/json"
	"fmt"
)

type Content struct {
	Owner   string
	Details []*Detail
}

type Detail struct {
	Phone string
	Email string
}

func (c *Content) SearchPhone(phone string) *Detail {
	for _, elem := range c.Details {
		if elem.Phone == phone {
			return elem
		}
	}
	return nil
}

func (c *Content) SearchEmail(email string) *Detail {
	for _, elem := range c.Details {
		if elem.Email == email {
			return elem
		}
	}
	return nil
}

func main() {
	encoded := `{
		  "Owner": "abc",
		  "Details": [
			{
			    "Phone": "1111",
			    "Email": "@gmail"
			},
			{
			    "Phone": "2222",
			    "Email": "@yahoo"
			}
		   ]
		}`

	// 解码json对象
	u := &Content{}
	if err := json.Unmarshal([]byte(encoded), u); err != nil {
		panic(err)
	}

	// 打印出Email和Phone
	fmt.Printf("Email: %s\n", u.SearchPhone("1111").Email)
	fmt.Printf("Phone: %s\n", u.SearchEmail("@yahoo").Phone)
}
英文:

Small details: you are referencing twice: you give the address of the address of the object to json.Unmarshal. Just give the address.

` allows for multiline, no need to split your json input.

I don't know what you where trying to achieve with u.Details[Phone:"1111"].Email, but this is no Go syntax. your Details member is a slice off Detail. A slice is similar to an array and can be accessed by index.

Also, your json does not match your object structure. If you want to have multiple details in one content, then it needs to be embed in an array ([ ])

You could do something like this: (http://play.golang.org/p/OP1zbPW_wk)

package main

import (
	"encoding/json"
	"fmt"
)

type Content struct {
	Owner   string
	Details []*Detail
}

type Detail struct {
	Phone string
	Email string
}

func (c *Content) SearchPhone(phone string) *Detail {
	for _, elem := range c.Details {
		if elem.Phone == phone {
			return elem
		}
	}
	return nil
}

func (c *Content) SearchEmail(email string) *Detail {
	for _, elem := range c.Details {
		if elem.Email == email {
			return elem
		}
	}
	return nil
}

func main() {
	encoded := `{
		  "Owner": "abc",
		  "Details": [
			{
			    "Phone": "1111",
			    "Email": "@gmail"
			},
			{
			    "Phone": "2222",
			    "Email": "@yahoo"
			}
		   ]
		}`

	// Decode the json object
	u := &Content{}
	if err := json.Unmarshal([]byte(encoded), u); err != nil {
		panic(err)
	}

	// Print out Email and Phone
	fmt.Printf("Email: %s\n", u.SearchPhone("1111").Email)
	fmt.Printf("Phone: %s\n", u.SearchEmail("@yahoo").Phone)
}

huangapple
  • 本文由 发表于 2014年6月25日 00:17:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/24391601.html
匿名

发表评论

匿名网友

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

确定