登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-02 11:36:32 306浏览 收藏

一分耕耘,一分收获!既然都打开这篇《构建对象的 Json 对象并将其写入文件》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我正在尝试获取从 go api 接收的字符串数组,并将它们以奇怪的 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 (
    "fmt"
    "io/ioutil"
)

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

func main() {
    schema := `measures: {
    count: {
      type: 'count',
      drillmembers: [country]
    }
  },

  dimensions: {
  `
    a := []string{"country", "year", "gdppercap", "lifeexp", "pop", "continent"}
    var cubeschema string
    for _, s := range a {
        cubeschema += s + ": {\n\tsql: " + s + ",\n\ttype: `string`\n},\n"
    }

    filetext := []byte(schema + cubeschema + "}\n}")
    fmt.println(cubeschema)
    err := ioutil.writefile("test.js", filetext, 0644)
    check(err)
}

检查此代码。

我尝试执行第二部分:

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 对象并将其写入文件》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>