4

我有一个 json 文件(themes/snow/theme.json)

{
    Name:'snow',
    Bgimage:'background.jpg',
    Width:600,
    Height:400,
    Itemrotation:'20,40',
    Text:{
        Fontsize:12,
        Color:'#ff00ff',
        Fontfamily:'verdana',
        Bottommargin:20
    },
    Decoration:[
        {
            Path:'abc.jpg',
            X:2,
            Y:4,
            Rotation:0
        },
        {
            Path:'def.png',
            X:4,
            Y:22,
            Rotation:10
        }
    ]
}

我有一个解析 json 数据的文件

package main

import (
    "fmt"
    "os"
    "encoding/json"
    "io/ioutil"
    "log"
)

const themeDirectory    = "themes"
const themeJsonFile     = "theme.json"

type TextInfo struct {
    Fontsize        int
    Color           string
    Fontfamily      string
    Bottommargin    int
}

type DecoInfo struct {
    Path            string
    X               int
    Y               int
    Rotation        int
}

type ThemeInfo struct {
    Name            string
    Bgimage         string
    Width           int
    Height          int
    Itemrotation    string
    Text            textInfo
    Decoration      []decoInfo
}

func main() {
    var tinfo = parseTheme("snow")
        //use tinfo to build graphics
}

func parseTheme(themename string) themeInfo {
    abspath, _ := os.Getwd()
    filename :=  abspath + "/" + themeDirectory + "/" + themename + "/" + themeJsonFile

    //Check this file exists
    if _, error := os.Stat(filename); error != nil {
        if os.IsNotExist(error) {
            log.Fatal(filename + " does not exist")
            os.Exit(1)
        }
    } 

    filebyte, error := ioutil.ReadFile(filename) 
    if error != nil { 
        log.Fatal("Could not read file " + filename + " to parse")
        os.Exit(1) 
    } 

    var t themeInfo
    json.Unmarshal(filebyte, &t) 
    fmt.Println(t)
    return t
}

你可以看到我在结束前有 2 行

 fmt.Println(t)

我不确定为什么会打印

{  0 0  {0   0} []}

我希望它应该以可用的方式返回我的主题信息,以便我可以使用它进行进一步处理。我在这里做错了什么?

4

2 回答 2

16

由于 json 包使用反射来剖析你的结构,它只能看到导出的字段。您的所有字段名称都以小写字母开头,因此它们不会被导出。将名称更改为以大写字母开头,我怀疑它会开始为您工作。

于 2012-09-19T02:12:06.010 回答
9

您的 JSON 无效。JavaScript 允许单引号;JSON没有。此外,对象键必须用双引号引起来:

Valid:

{ "name": "Simon" }

Invalid:

{ name: "Simon" }
{ 'name': "Simon" }
{ "name": 'Simon' }

如果用双引号将 JSON 键和值括起来,您将看到预期的输出:

{snow background.jpg 600 400 20,40 {12 #ff00ff verdana 20} [{abc.jpg 2 4 0} {def.png 4 22 10}]}

例如,

const sampleTheme       = `{
    "Name":"snow",
    "Bgimage":"background.jpg",
    "Width":600,
    "Height":400,
    "Itemrotation":"20,40",
    "Text":{
        "Fontsize":12,
        "Color":"#ff00ff",
        "Fontfamily":"verdana",
        "Bottommargin":20
    },
    "Decoration":[
        {
            "Path":"abc.jpg",
            "X":2,
            "Y":4,
            "Rotation":0
        },
        {
            "Path":"def.png",
            "X":4,
            "Y":22,
            "Rotation":10
        }
    ]
}`

完整程序见:http ://play.golang.org/p/SLhaLbJcla

于 2012-09-19T03:42:41.183 回答