Arrays 构建Json对象并将其写入文件

Arrays 构建Json对象并将其写入文件,arrays,go,Arrays,Go,我试图获取从GoAPI接收的字符串数组,并将它们以奇怪的json列表格式写入文件。没有方括号[],因此我必须为数组中的每个字符串值创建一个“维度”。我试图使用类型/结构来实现这一点,但我一直被卡住(在Go中是新的)。我应该试着只使用地图,还是有办法做到这一点 这是我现在正在使用的代码: package main import ( "fmt" "io/ioutil" ) type Dimension struct { SQL, Type string } type M

我试图获取从GoAPI接收的字符串数组,并将它们以奇怪的json列表格式写入文件。没有方括号[],因此我必须为数组中的每个字符串值创建一个“维度”。我试图使用类型/结构来实现这一点,但我一直被卡住(在Go中是新的)。我应该试着只使用地图,还是有办法做到这一点

这是我现在正在使用的代码:

package main

import (
    "fmt"
    "io/ioutil"
)

type Dimension struct {
    SQL, Type string
}

type Measure struct {
    Type         string
    DrillMembers []string
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func main() {
    a := []string{"country", "year", "gdpPercap", "lifeExp", "pop", "continent"}
    cubeSchema := "measures: {\n  count: {\n    type: `count`,\n    drillMembers: "
    for i, s := range a {
        cubeSchema += s
        fmt.Println(cubeSchema)
        fmt.Println(i)
    }

    fileText := []byte(cubeSchema)
    fmt.Println(cubeSchema)
    err := ioutil.WriteFile("test.js", fileText, 0644)
    check(err)
}

这就是我需要输出的外观:

measures: {
    count: {
      type: `count`,
      drillMembers: [country]
    }
  },

  dimensions: {
    country: {
      sql: `country`,
      type: `string`
    },

    year: {
      sql: `year`,
      type: `string`
    },

    gdppercap: {
      sql: `gdppercap`,
      type: `string`
    },

    lifeexp: {
      sql: `lifeexp`,
      type: `string`
    },

    pop: {
      sql: `pop`,
      type: `string`
    },

    continent: {
      sql: `continent`,
      type: `string`
    }
  }
现在,我一直被以下输出所困扰:

measures: {
  count: {
    type: `count`,
    drillMembers: countryyeargdpPercaplifeExppopcontinent

检查此代码。

我尝试执行第二部分:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    a := []string{"country", "year", "gdpPercap", "lifeExp", "pop", "continent"}
    var items map[string]sqlType
    items = make(map[string]sqlType)

    for _, v := range a {
        items[v] = sqlType{SQL: v, Type: "string"}
    }

    dimensions := dimensions{Dimensions: items}

    bytes, err := json.Marshal(dimensions)

    if err != nil {
        panic(err)
    }
    c := string(bytes)

    fmt.Println(c)

}

type sqlType struct {
    SQL  string `json:"sql"`
    Type string `json:"type"`
}

type dimensions struct {
    Dimensions map[string]sqlType `json:"dimensions"`
}

“这就是我需要输出的样子”这不是JSON。您希望数据采用什么格式?它既不是JSON也不是YAML。如果您想保持非标准格式,您必须编写与您所编写的类似的自定义代码。另外,我不认为使用地图而不是结构会更容易。
package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    a := []string{"country", "year", "gdpPercap", "lifeExp", "pop", "continent"}
    var items map[string]sqlType
    items = make(map[string]sqlType)

    for _, v := range a {
        items[v] = sqlType{SQL: v, Type: "string"}
    }

    dimensions := dimensions{Dimensions: items}

    bytes, err := json.Marshal(dimensions)

    if err != nil {
        panic(err)
    }
    c := string(bytes)

    fmt.Println(c)

}

type sqlType struct {
    SQL  string `json:"sql"`
    Type string `json:"type"`
}

type dimensions struct {
    Dimensions map[string]sqlType `json:"dimensions"`
}