doujiurong7210 2018-07-23 00:19
浏览 4351

如何在YAML文件中将JSON数据集作为字符串传递?

how can i have a key in YAML format which accepts a json data as a string? I tried to put the json data in quotes but it reads as a list of map.

test1: '[{'a':'abd','asxs': 'csd','sx':'sft'}]'
test2: default

I want yaml to read 'test1' as string rather than a list of dicts/maps. how do i get it?

I get below error:

Failed while parsing request input: field "Request" field "string" can only parse list of bytes or characters, invalid element: map["a":"abd" "asxs":"csd" "sx":"sft"]

  • 写回答

1条回答 默认 最新

  • dplp5928 2018-07-23 03:19
    关注

    Define test1 as a string instead of []map[string]string. you can use go-yaml for parsing and loading your data into your struct's fields. hope I understood your question correctly, if not please leave a comment. here's a complete example (make sure to define your vars with capital letter in the beginning e.g. Test1)

    package main
    
    import (
        "log"
        "gopkg.in/yaml.v2"
    )
    
    func main() {
        var data = `
       test1: "[{ 'a': 'abd', 'ases': 'cad', 'sx': 'sft' }]"
       test2: default
       `
    
        type T struct {
            Test1 string
            Test2 string
        }
        t := T{}
        err := yaml.Unmarshal([]byte(data), &t)
        if err != nil {
            log.Fatalf("error: %v", err)
        }
    
        println("test1 value: ", t.Test1)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?