zoukankan      html  css  js  c++  java
  • map[interface {}]interface {} yaml文件解码

    map[interface {}]interface {}
     
    map[interface{}]interface{} on a map[string]interface{} · Issue #139 · go-yaml/yaml https://github.com/go-yaml/yaml/issues/139
     
    f1: hi世界
    f2:
      f3: value
      f4: [42, 1024]
      f5: [a b]
      f6:
        f7: 77
        f8: ok
    

      

     
    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    
    	"gopkg.in/yaml.v3"
    )
    
    func main() {
    	var err error
    	m := make(map[string]interface{})
    	var bs []byte
    	bs, err = ioutil.ReadFile("a.yaml")
    	if err != nil {
    		panic(err)
    	}
    	err = yaml.Unmarshal(bs, &m)
    	if err != nil {
    		panic(err)
    	}
    	f1v := m["f1"].(string)
    	var f3v string
    	var f4v []int
    	var f5v []string
    	var f6v map[string]interface{}
    
    	for k, v := range m["f2"].(map[string]interface{}) {
    		switch k {
    		case "f3":
    			f3v = v.(string)
    		case "f4", "f5":
    			l := v.([]interface{})
    			if k == "f4" {
    				for _, u := range l {
    					f4v = append(f4v, u.(int))
    				}
    			}
    			if k == "f5" {
    				for _, u := range l {
    					f5v = append(f5v, u.(string))
    				}
    			}
    		case "f6":
    			f6v = v.(map[string]interface{})
    		default:
    		}
    	}
    	
    	fmt.Println("---->", f1v, f3v, f4v, f5v, f6v)
    }
    

      

    go run *go
    ----> hi世界 value [42 1024] [a b] map[f7:77 f8:ok]

     
     
     
     
  • 相关阅读:
    [ts]类
    【跨域】jsonp的实现
    [ts]基础类型
    在Crystal Report中将数字转为英文
    合并 GridView 的单元格
    C#动态加载DLL
    在网页处理按键事件
    SQL Server 2005 中新CTE语法 递归性能测试
    连接远程服务器共享
    Asp.net 文件下载
  • 原文地址:https://www.cnblogs.com/rsapaper/p/15798414.html
Copyright © 2011-2022 走看看