为什么我将地图转换为JSON时,地图中包含的列表值在转换为JSON后变为空?

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

Why do I convert map to json, map includes list values which is nothing after converting to json

问题

func Test_JsonTtransfer(t *testing.T) {

uid := "306"
phoneList := list.New()
phoneList.PushBack("18513622928")
fmt.Println("phoneList=======", phoneList.Len())


jsonPhoneList, err := json.Marshal(phoneList)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Println("jsonPhoneList=======", string(jsonPhoneList))

idCardList := list.New()
idCardList.PushBack("230405197608040640")

request := make(map[string]interface{})
request["uid"] = uid

request["phones"] = phoneList

request["id_cards"] = idCardList

json, err := json.Marshal(request)
if err != nil {
    fmt.Println("error:", err)
}

fmt.Println("json=======", json)
fmt.Println("json=======", string(json))

}

输出:

D:/Sys/server/Go\bin\go.exe test -v golang-test/com/http/test -run ^Test_JsonTtransfer$
phoneList======= 1
jsonPhoneList======= {}
json======= [123 34 105 100 95 99 97 114 100 115 34 58 123 125 44 34 112 104 111 110 101 115 34 58 123 125 44 34 117 105 100 34 58 34 51 48 54 34 125]
json======= {"id_cards":{},"phones":{},"uid":"306"}
ok golang-test/com/http/test 0.482s

Phones应该是列表值,但是为空。帮我看看。

英文:
func Test_JsonTtransfer(t *testing.T) {

	uid := "306"
	phoneList := list.New()
	phoneList.PushBack("18513622928")
	fmt.Println("phoneList=======", phoneList.Len())


	jsonPhoneList, err := json.Marshal(phoneList)
	if err != nil {
	    fmt.Println("error:", err)
	}
	fmt.Println("jsonPhoneList=======", string(jsonPhoneList))

	idCardList := list.New()
	idCardList.PushBack("230405197608040640")

	request := make(map[string]interface{})
	request["uid"] = uid

	request["phones"] = phoneList

	request["id_cards"] = idCardList

	json, err := json.Marshal(request)
	if err != nil {
	    fmt.Println("error:", err)
	}

	fmt.Println("json=======", json)
	fmt.Println("json=======", string(json))
}

Output:

>D:/Sys/server/Go\bin\go.exe test -v golang-test/com/http/test -run ^Test_JsonTtransfer$
>phoneList======= 1
>jsonPhoneList======= {}
>json======= [123 34 105 100 95 99 97 114 100 115 34 58 123 125 44 34 112 104 111 110 101 115 34 58 123 125 44 34 117 105 100 34 58 34 51 48 54 34 125]
>json======= {"id_cards":{},"phones":{},"uid":"306"}
>ok golang-test/com/http/test 0.482s

Phones should be list values, but nothing. Help me.

答案1

得分: 1

因为List类型没有导出的字段,并且该类型没有实现Marshaler接口,所以List值始终会被编组为文本{}

一个简单的修复方法是使用切片而不是列表:

var phoneList []string
phoneList = append(phoneList, "18513622928")
fmt.Println("phoneList=======", len(phoneList))

playground示例

英文:

Because the List type has no exported fields and the type does not implement the Marshalerinterface, List values always marshal to the text {}.

A simple fix is to use a slice instead of a list:

var phoneList []string
phoneList = append(phoneList, "18513622928")
fmt.Println("phoneList=======", len(phoneList)

playground example

huangapple
  • 本文由 发表于 2017年5月28日 13:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/44224133.html
匿名

发表评论

匿名网友

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

确定