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} []}

使用可能な方法でthemeInfoが返され、さらに処理するために使用できるようになると思います。ここで何が間違っているのでしょうか。

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 に答える