dqo88037
2018-09-05 19:19
浏览 239

如何在golang中键入混合类型的数组?

I'm writing out a map which I need to convert to JSON, and one of the required values is an array of mixed type.

var models = map[string]interface{} {
   ...
   "req": [[0, "all", [0]]],
   ...
}

Is there a way to assign a type to this nested array? Or is there a better approach for creating this JSON than a map?

Edit: Getting closer I think, unsure what to do about the nested arrays

var models = map[string]interface{} {
       ...
       "req": []interface{}{[[0, "all", [0]]]},
       ...
    }

Edit: The entire JSON I'm looking to create, I'm just stuck on the "req" key, but since it was requested:

{
    1388596687391: {
      veArs: [],
      name: 'Basic-f15d2',
      tags: ['Tag'],
      did: 1435588830424,
      usn: -1,
      req: [[0, 'all', [0]]],
      flds: [
        {
          name: 'Front',
          media: [],
          sticky: false,
          rtl: false,
          ord: 0,
          font: 'Arial',
          size: 20
        },
        {
          name: 'Back',
          media: [],
          sticky: false,
          rtl: false,
          ord: 1,
          font: 'Arial',
          size: 20
        }
      ],
      sortf: 0,
      latexPre:
        '\\documentclass[12pt]{article}
\\special{papersize=3in,5in}
\\usepackage[utf8]{inputenc}
\\usepackage{amssymb,amsmath}
\\pagestyle{empty}
\\setlength{\\parindent}{0in}
\\begin{document}
',
      tmpls: [
        {
          name: 'Card 1',
          qfmt: questionFormat,
          did: null,
          bafmt: '',
          afmt: answerFormat,
          ord: 0,
          bqfmt: ''
        }
      ],
      latexPost: '\\end{document}',
      type: 0,
      id: 1388596687391,
      css,
      mod: 1435645658
    }
  }

图片转代码服务由CSDN问答提供 功能建议

我正在写一张地图,我需要将其转换为JSON,并且必需的值之一是数组

  var models = map [string] interface {} {
 ... 
“ req”:[[0,“ ​​all”,[0  ]]],
 ... 
} 
   
 
 

是否可以为该嵌套数组分配类型? 还是有一个比映射更好的创建JSON的方法?

编辑:我想更近一点,不确定如何处理嵌套数组

 变量模型= map [string] interface {} {
 ... 
“ req”:[] interface {} {[[0,“ ​​all”,[0]  ]]},
 ... 
} 
   
 
 

编辑:我要创建的整个JSON,我是 m只是停留在“ req”键上,但由于有人要求输入它:

  {
 1388596687391:{
 veArs:[],
名称:'Basic-  f15d2',
标签:['Tag'],
完成了:1435588830424,
 usn:-1,
请求:[[0,'all',[0]]],
 flds:[\  n {
名称:'Front',
媒体:[],
粘性:false,
 rtl:false,
 ord:0,
字体:'Arial',
大小:20 
  },
 {
名称:“ Back”,
媒体:[],
粘性:false,
 rtl:false,
 ord:1,
字体:“ Arial”,
大小:  20  
} 
],
 sortf:0,
 latexPre:
'\\ documentclass [12pt] {article} 
 \\ special {papersize = 3in,5in} 
 \\ usepackage [utf8] {  inputenc} 
 \\ usepackage {amssymb,amsmath} 
 \\ pagestyle {empty} 
 \\ setlength {\\ parindent} {0in} 
 \\ begin {document} 
',
 tmpls:[  
 {
名称:“卡1”,
 qfmt:questionFormat,
做到了:空,
 bafmt:“”,
 afmt:answerFormat,
 ord:0,
 bqfmt:“ \  n} 
],
 latexPost:'\\ end {document}',
类型:0,
 ID:1388596687391,
 CSS,
 mod:1435645658 
} 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsyk33753 2018-09-05 19:29
    已采纳

    Slice/array/map literals all work the same way, as explained in the tour - each value literal just needs the type specified when the containing type doesn't already specify the type (i.e. a map/slice/array of an interface type), and every list of values is enclosed in braces {}, not brackets [] as in JSON:

       "req": []interface{}{[]interface{}{[]interface{}{0, "all", []interface{}{0}}}},
    

    Note that the last element could also be written as []int{0}.

    点赞 打赏 评论
  • doushan2311 2018-09-05 19:28

    Here is a basic example of doing it:

    package main
    
    import "fmt"
    
    func main() {
      primes := [6]int {2, 3, 5, 7, 11, 13}
      mixed := []interface{} {"foo", 10, primes }
      //fmt.Println(mixed...)
    
      test := map[string]interface{} {
        "omar":mixed,
      }
    
      fmt.Println(test["omar"])
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题