json - Go语言中带有结果对象的动态结构

标签 json go beego

我想创建具有动态数据结构的结果对象 json,ex 在 func1 中,结果是这样的

{
  'result': 'this is result',
  'content': {
    'func1'  : 'value',
    'some_desc_func1': 'value'
  }
}

和 func2 可能结果只是(专注于内容)像这样

{
  'result': 'this is result',
  'content': {
    'func2'  : 'value'
  }
}

作为引用 https://stackoverflow.com/a/35657622/4476788 ,我想用结果的一个键显示结果 json。

像这样

{
  'result': 'this is result',
  'content': {
    'key'  : 'value'
  }
}

不是这样的

[
  {
    'result_1' : 'answer 1'
  },
  {
    'result_2' : 'answer 2'
  }
]

我尝试更新答案的 play ground 但它显示错误

type Brand struct {
   Name string
}

var database map[string]interface{}

func init() {
  database = make(map[string]interface{})

  brands := make([]Brand, 1)
  brands = Brand{"Gucci"}

  database["brands"] = brands
} 

你可以尝试在这里运行 https://play.golang.org/p/mKCwKEVI7E

显示错误

tmp/sandbox651609402/main.go:22:不能使用品牌字面量(品牌类型)作为赋值中的类型[]品牌

最佳答案

第 22 行应该是:brands = []Brand{Brand{"Gucci"}}

关于json - Go语言中带有结果对象的动态结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41054288/

相关文章:

php - 列出 dropbox 文件夹中的文件

json - 当 'Content-Type' 设置为 'application/json' 时,无法发送 post 请求

javascript - 不允许使用 AngularJS 和 Go POST 请求方法

c# - Go语言中Char到Ascii int的转换

windows - Beego(戈朗): Create new project via CLI in Windows

web - 有没有办法在代码更改后将 Controller 热重新加载到 Beego 中?

sql - 使用 JSON_BUILD_OBJ 从 Postgres 返回 JSON

html - 当键是整数字符串时,如何使用 *ng-For 在 ionic 中显示 JSON 数组?

Golang 检索应用程序正常运行时间

google-app-engine - 在 app.yaml 中混合脚本和静态