亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

構建對象的 Json 對象并將其寫入文件

構建對象的 Json 對象并將其寫入文件

Go
智慧大石 2022-06-06 14:50:46
我正在嘗試獲取從 Go API 接收到的字符串數組,并將它們以奇怪的 json 列表格式寫入文件。沒有括號 [],所以我必須為數組中的每個字符串值創建一個“維度”。我正在嘗試使用類型/結構來做到這一點,但我一直卡住(Go 的新功能)。我應該嘗試只使用地圖,還是有辦法做到這一點?這是我現在使用的代碼:package mainimport (    "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
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

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)

}

檢查此代碼。


查看完整回答
反對 回復 2022-06-06
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

我試著做第二部分:


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"`

}


查看完整回答
反對 回復 2022-06-06
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號