英文:
Unable to parse a complex json in golang
问题
{
"period": "yy",
"exec_period": {
"start": {
"month": 1,
"week": 2,
"day": 3,
"hour": 4,
"minute": 5
},
"end": {
"month": 6,
"week": 7,
"day": 8,
"hour": 9,
"minute": 10
}
},
"backup": [
{
"local_dir": "directoryLo1",
"server_dir": "directoryLo2",
"server_host": "domaineName"
},
{
"local_dir": "directoryLo1",
"server_dir": "directorySe2",
"server_host": "domaineName"
}
],
"incremental_save": "1Y2M"
}
英文:
I want to parse this JSON (in config/synch.conf):
{
	"period" :"yy",
	"exec_period" :  
		{
			"start" : {
				"month" : 1,
				"week" : 2,
				"day" : 3,
				"hour" : 4,
				"minute" : 5
			},
			"end" :	{
				"month" : 6,
				"week" : 7,
				"day" : 8,
				"hour" : 9,
				"minute" : 10
			}
		},
	"backup" : [
		{
			"local_dir" : "directoryLo1",
			"server_dir" :	"directoryLo2",
			"server_host" : "domaineName"
		},
		{
			"local_dir" : "directoryLo1",
			"server_dir" :	"directorySe2",
			"server_host" : "domaineName"
		}
	],
	"incremental_save" : "1Y2M"
}
With this programm:
package main
import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)
func main() {
	content, err := ioutil.ReadFile("config/synch.conf")
	if err == nil {
		
		type date struct{
			month float64
			week float64
			day float64
			hour float64
			minute float64
		}
		
		type period struct{
			start date
			end date
		}
		
		type backupType []struct{
			local_dir string
			server_dir string
			server_host string
		}
		
		type jason struct{
			period string
			exec_period period
			backup backupType
			incremental_save string
		}
		
		var parsedMap jason
		
		err := json.Unmarshal(content, &parsedMap)
		
		if err!= nil {
			fmt.Println(err)
		}
		
		fmt.Println(parsedMap)
	} else {
		panic(err)
	}
}
Which doesn't work as expected, as the output is:
{ {{0 0 0 0 0} {0 0 0 0 0}} [] }
Here is the same example at play.golang.org
http://play.golang.org/p/XoMJIDIV59
I don't know if this is possible with go, but I wanted to get the value of the json.Unmarshal function stored in a map[string]interface{} (or another object that allows that) where I could access, for example, the value of the minute's end (10) like this: parsedMap["exec_period"]["end"]["minute"], but I don't uderstand the "Generic JSON withinterface{}" part of [JSON and Go][1] at golang.org
[1]: http://golang.org/doc/articles/json_and_go.html "JSON and Go"
答案1
得分: 13
你的代码很好,除了json包只能处理导出的字段之外。
如果你将每个字段名的首字母大写,一切都会正常工作:
type date struct {
    Month  float64
    Week   float64
    Day    float64
    Hour   float64
    Minute float64
}
type period struct {
    Start date
    End   date
}
type backupType []struct {
    Local_dir   string
    Server_dir  string
    Server_host string
}
type jason struct {
    Period           string
    Exec_period      period
    Backup           backupType
    Incremental_save string
}
虽然可以将其编组为map[string]interface{},但如果数据具有固定的结构(如你的问题中的结构),你的解决方案可能更可取。使用interface{}将需要类型断言,可能会变得混乱。你的示例将如下所示:
parsedMap["exec_period"].(map[string]interface{})["end"].(map[string]interface{})["minute"].(float64)
英文:
Your code is fine except that the json package can only work with exported fields.
Everything will work if you capitalize the first letter for each field name:
type date struct {
    Month  float64
    Week   float64
    Day    float64
    Hour   float64
    Minute float64
}
type period struct {
    Start date
    End   date
}
type backupType []struct {
    Local_dir   string
    Server_dir  string
    Server_host string
}
type jason struct {
    Period           string
    Exec_period      period
    Backup           backupType
    Incremental_save string
}
While it is possible to marshal into a map[string]interface{}, if the data has a set structure (such as the one in your question), your solution is most likely preferable. Using interface{} would require type assertions and might end up looking messy. Your example would look like this:
parsedMap["exec_period"].(map[string]interface{})["end"].(map[string]interface{})["minute"].(float64)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论