doushiposong30622 2012-09-19 02:00
浏览 485
已采纳

Golang:json.Unmarshal没有正确返回数据

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?

  • 写回答

2条回答 默认 最新

  • doubu1964 2012-09-19 03:42
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?