无法解析golang中的复杂json [英] Unable to parse a complex json in golang

查看:110
本文介绍了无法解析golang中的复杂json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析这个JSON(在config / synch.conf中):

  {
period :yy,
exec_period:
{
开始:{
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
}

有了这个程序:

  package main 

import(
encoding / json
fmt
io / ioutil


func main(){
content,err:= ioutil.ReadFile(config / synch.conf)
if err = =无{

类型日期结构{
月份float64
星期float64
日期float64
小时float64
分钟float64
}

类型句点结构{
开始日期
结束日期
}

类型backupType []结构{
local_dir字符串
server_dir字符串
server_host字符串
}

类型jason结构{
句点字符串
exec_period句点
备份类型
incremental_save字符串
}

var parsedMap jason

err:= json.Unmarshal(content,& parsedMap)

if err!= nil {
fmt.Println(err)


fmt.Println(parsedMap)
} else {
panic(err)
}

}

无法按预期工作,因为输出为:

  {{{0 0 0 0 0} {0 0 0 0 0}} []} 

以下是play.golang.org

http://play.golang.org/p/XoMJIDIV59



我不知道这是否可行,但我想要获取存储在map [string] interface {}(或允许的其他对象)中存储的 json.Unmarshal 函数的值,例如, (10)的值如下所示: parsedMap [exec_period] [end] [minute] ,但我不理解通用JSON and Go 在Golang的 JSON和Go 中的部分内容{} .org

解决方案

你的代码很好,除了 json

如果您为每个字段名称的第一个字母进行大写,那么一切都可以工作:

 类型日期结构{
月份float64
星期float64
日期float64
小时float64
分钟float64
}

类型句点结构{
开始日期
结束日期
}

类型backupType []结构{
Local_dir字符串
Server_dir字符串
Server_host字符串
}

类型jason struct {
句点字符串
Exec_period句点
备份备份类型
Incremental_save字符串
}

虽然它是如果数据有一个设置结构(比如你的问题中的那个),可能会编入一个 map [string] interface {} ,你的解决方案最可能是更可取的。使用interface {}将需要类型断言,并可能最终显得杂乱无章。您的示例如下所示:

  parsedMap [exec_period]。(map [string] interface {})[end ]。(map [string] interface {})[minute]。(float64)


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 at golang.org

解决方案

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)

这篇关于无法解析golang中的复杂json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆