Golang:json.Unmarshal没有正确返回数据 [英] Golang: json.Unmarshal is not returning data correctly
问题描述
我有一个json文件(themes / snow / theme.json)
{
名称:'snow ',
Bgimage:'background.jpg',
宽度:600,
高度:400,
物品转录:'20,40',
文字:{
Fontsize:12,
颜色:'#ff00ff',
Fontfamily:'verdana',
Bottommargin:20
},
装饰:[
{
路径:'abc.jpg',
X:2,
Y:4,
循环:0
},
{
路径:'def.png',
X:4,
Y:22,
轮换:10
}
]
}
我有一个解析json数据的文件
package main
import(
fmt
os
encoding / json
io / ioutil
log
)
const themeDirectory =themes
const themeJsonFile =theme.json
type TextInfo struct {
Fontsize int
颜色字符串
Fontfamily字符串
Bottommargin int
}
类型DecoInfo结构{
路径字符串
X int
Y int
旋转int
}
类型ThemeInfo结构{
名称字符串
Bgimage字符串
宽度int
高度int
Itemrotation字符串
文本文本信息
装饰[]装饰信息
}
func main(){
var tinfo = parseTheme(snow)
//使用tinfo构建图形
}
func parseTheme(themename string)themeInfo {
abspath,_:= os.Getwd()
filename:= abspath +/+ themeDirectory +/+ themename +/+ themeJsonFile
//检查此文件是否存在
if _,error:= os.Stat(filename);错误!=无{
如果os.IsNotExist(错误){
log.Fatal(文件名+不存在)
os.Exit(1)
}
filebyte,error:= ioutil.ReadFile(filename)
if error!= nil {
log.Fatal(Could not read file+ filename + (1)
$ b var t themeInfo
json.Unmarshal(filebyte,& t)
fmt。 Println(t)
return t
}
你可以看到我有2
fmt.Println(t)
我不确定它为什么打印
{0 0 {0 0} []}
我希望它能以可用的方式返回给我themeInfo,这样我就可以使用它进行进一步处理。我在这里做错了什么? 您的JSON无效。 JavaScript允许单个报价; JSON 不。此外,对象键必须用双引号括起来:
有效:
{name: Simon}
无效:
{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,
文字:{
Fontsize :12,
颜色:#ff00ff,
Fontfamily:verdana,
Bottommargin:20
},
装饰 :[
{
路径:abc.jpg,
X:2,
Y:4,
旋转:0
},
{
路径:def.png,
X:4,
Y:22,
轮换:10
}
]
}`
完整节目请参阅: http://play.golang.org/p/SLhaLbJcla
p>I have a json file (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
}
]
}
And I have a file that parse the json data
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
}
You can see I have 2 lines before the end
fmt.Println(t)
I am not sure why does it print
{ 0 0 {0 0} []}
I expect it should return me themeInfo in a usable way, so that I can use it for further processing.What am I doing wrong here?
Your JSON is not valid. JavaScript allows single quotes; JSON does not. Further, the object keys must be double quoted:
Valid:
{ "name": "Simon" }
Invalid:
{ name: "Simon" }
{ 'name': "Simon" }
{ "name": 'Simon' }
If you wrap your JSON keys and values with double quotes, you'll see the expected output:
{snow background.jpg 600 400 20,40 {12 #ff00ff verdana 20} [{abc.jpg 2 4 0} {def.png 4 22 10}]}
For exmaple,
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
}
]
}`
For the full program, see: http://play.golang.org/p/SLhaLbJcla
这篇关于Golang:json.Unmarshal没有正确返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!