2012-09-19 124 views
4

我有一个JSON文件(主题/雪地/ theme.json)Golang:json.Unmarshal没有返回数据正确

{ 
    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 
} 

你可以看到我有结束

fmt.Println(t) 

我不知道之前的2线为什么它打印

{ 0 0 {0 0} []} 

我希望它应该以可用的方式返回我themeInfo,以便我可以使用它进行进一步processing.What我在这里做错了吗?

+0

** **从未忽略错误。如果你至少已经打印/记录/整理了来自'json.Unmarshal'的'error'结果,它会告诉你你确切的问题,或者直接告诉你(和我们)。 –

回答

8

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

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

+1

谢谢斯里兰卡。这是我的不好。我完全错过了它。现在它正在工作。 – JVK

16

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

+1

我的所有三个结构中的所有字段名都以大写字母的第一个字母进行了更改。键入textInfo结构{ \t字体大小\t \t INT \t颜色\t \t \t串 \t fontFamily中\t \t串 \t Bottommargin \t INT } 类型decoInfo结构{ \t路径\t \t \t串 \t X \t \t \t \t一世NT \tŸ\t \t \t \t INT \t旋转\t \t INT } 型themeInfo结构{ \t名称\t \t \t串 \t Bgimage \t \t串 \t宽度\t \t \t INT \t身高\t \t \t INT \t \t Itemrotation串 \t \t文本\t \t textInfo \t \t装饰\t [] decoInfo }我还修改的所有键的第一个字母在JSON为大写。但我仍然得到相同的结果{0 0 {0 0} []} – JVK