Golang – 使用/迭代JSON解析的映射

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

Golang - using/iterating through JSON parsed map

问题

使用PHP和JavaScript(以及Node.js),解析JSON是一个非常简单的操作。从外观上看,Go语言要复杂得多。请考虑下面的示例代码:

package main

import (
	"encoding/json"
	"fmt"
)

type fileData struct {
	tn   string
	size int
}

type jMapA map[string]string
type jMapB map[string]fileData

func parseMapA() {
	var dat jMapA
	s := `{"lang":"Node","compiled":"N","fast":"maybe"}`
	if err := json.Unmarshal([]byte(s), &dat); err != nil {
		panic(err)
	}

	fmt.Println(dat)
	for k, v := range dat {
		fmt.Println(k, v)
	}
}

func parseMapB() {
	var dat jMapB
	s := `{"f1":{"tn":"F1","size":1024},"f2":{"tn":"F2","size":2048}}`
	if err := json.Unmarshal([]byte(s), &dat); err != nil {
		panic(err)
	}

	fmt.Println(dat)
	for k, v := range dat {
		fmt.Println(k, v)
	}
}

func main() {
	parseMapA()
	parseMapB()
}

parseMapA() 函数的调用将返回:

map[lang:Node compiled:N fast:maybe]
lang Node
compiled N
fast maybe

然而,parseMapB() 函数的返回结果是:

map[f1:{F1 1024} f2:{F2 2048}]
f2 {F2 2048}
f1 {F1 1024}

我刚开始接触Go语言,所以我想我在这里做错了什么。然而,我不知道具体是什么错误。更一般地说,在Go语言中,如何将下面的Node代码转换为等效的Go代码:

for (p in obj) {
  doSomethingWith(obj[p]);
}

请注意,我只需要翻译代码,不需要回答其他问题。

英文:

With PHP and JavaScript (and Node) parsing JSON is a very trivial operation. From the looks of it Go is rather more complicated. Consider the example below:

package main

import ("encoding/json";"fmt")

type fileData struct{
 tn string
 size int
}

type jMapA map[string] string
type jMapB map[string] fileData

func parseMapA(){
 var dat jMapA
 s := `{"lang":"Node","compiled":"N","fast":"maybe"}`
 if err := json.Unmarshal([]byte(s), &dat); err != nil {
  panic(err)
 }

 fmt.Println(dat);
 for k,v := range dat{
  fmt.Println(k,v)
 }
}

func parseMapB(){
 var dat jMapB
 s := `{"f1":{"tn":"F1","size":1024},"f2":{"tn":"F2","size":2048}}`
 if err := json.Unmarshal([]byte(s), &dat); err != nil {
  panic(err)
 }

 fmt.Println(dat);
 for k,v := range dat{
  fmt.Println(k,v)
 }
}

func main() {
 parseMapA()
 parseMapB()	
}

The parseMapA() call obligingly returns:

map[lang:Node Compiled:N fast:maybe]
lang Node
compiled N
fast maybe

However, parseMapB() returns:

map[f1:{ 0}, f2:{ 0}]
f2 { 0}
f1 { 0}

I am into my first few hours with Go so I imagine I am doing something wrong here. However, I have no idea what that might be. More generally, what would the Go equivalent of the Node code

for(p in obj){
  doSomethingWith(obj

); }

be in Go?

答案1

得分: 2

在Go语言中,只有具有公开字段的结构体才能进行解组(unmarshaling),即以大写字母开头的字段。

将你的第一个结构体改为:

type fileData struct{
    Tn   string
    Size int
}

请参考 http://play.golang.org/p/qO3U7ZNrNs 获取修复后的示例代码。

此外,如果你打算将该结构体编组(marshal)回JSON格式,你会注意到生成的JSON使用了大写字段,这可能不是你想要的。

你需要添加字段标签(field tags):

type fileData struct{
    Tn   string `json:"tn"`
    Size int    `json:"size"`
}

这样,编组函数(Marshal function)将使用正确的字段名。

英文:

In Go, unmarshaling works only if a struct has exported fields, ie. fields that begin with a capital letter.

Change your first structure to:

type fileData struct{
    Tn string
    Size int
}

See http://play.golang.org/p/qO3U7ZNrNs for a fixed example.

Moreover, if you intend to marshal this struct back to JSON, you'll notice that the resulting JSON use capitalized fields, which is not what you want.

You need to add field tags:

type fileData struct{
    Tn   string `json:"tn"`
    Size int    `json:"size"`
}

so the Marshal function will use the correct names.

huangapple
  • 本文由 发表于 2015年6月17日 23:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/30896040.html
匿名

发表评论

匿名网友

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

确定