我有一个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我在这里做错了吗?
** **从未忽略错误。如果你至少已经打印/记录/整理了来自'json.Unmarshal'的'error'结果,它会告诉你你确切的问题,或者直接告诉你(和我们)。 –