[GO]json解析到map
package main import (
"encoding/json"
"fmt"
) var str string func main() {
m := make(map[string]interface{}, )
jsonbuf := `{"company":"zyg","isok":true,"price":5.55,"subjects":["go","python","java"]}`
err := json.Unmarshal([]byte(jsonbuf), &m)
if err != nil {
return
}
for key, value := range m{
switch data := value.(type) {
case string:
str = data
fmt.Printf("m[%s] value type is string, = %s\n", key, str)
case float64:
fmt.Printf("m[%s] value type is float64, = %f\n", key, data)
case bool:
fmt.Printf("m[%s] value type is bool, = %v\n", key, data)
case []interface{}:
fmt.Printf("m[%s] value type is []interface{}, = %v\n", key, data)
}
}
}
执行的结果为
m[isok] value type is bool, = true
m[price] value type is float64, = 5.550000
m[subjects] value type is []interface{}, = [go python java]
m[company] value type is string, = zyg
这里可以看到,将json解析到map与解析到结构各有各的好处,在声明上,结构体需要声明结构类型,而map只需要一个make函数,但是一旦得到了值以后,结构休的方式可以直接操作,map方式需要一个一个进行断言判断才行
[GO]json解析到map的更多相关文章
- go语言之进阶篇json解析到map
1.json解析到map(通过类型断言,找到值和value类型) 示例: package main import ( "encoding/json" "fmt" ...
- json解析转map
HashMap<String, Object> map = new HashMap<String, Object>(); JSONObject jsonObject = ...
- golang json解析到map中
package main import ( "fmt" "encoding/json" ) type ItemMessage struct { ItemType ...
- 前台JSON对象传给springmvc,解析为map对象
前台JSON对象传给springmvc,解析为map对象 javascript: $.ajax({ url : url, method : 'post', contentType : 'applica ...
- Android okHttp网络请求之Json解析
前言: 前面两篇文章介绍了基于okHttp的post.get请求,以及文件的上传下载,今天主要介绍一下如何和Json解析一起使用?如何才能提高开发效率? okHttp相关文章地址: Android o ...
- Android总结之json解析(FastJson Gson 对比)
前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...
- Json解析工具Jackson(简单应用)
原文http://blog.csdn.net/nomousewch/article/details/8955796 概述 Jackson库(http://jackson.codehaus.org),是 ...
- Tomjson - 一个"短小精悍"的 json 解析库
Tomjson,一个"短小精悍"的 json 解析库,tomjson使用Java语言编写,主要作用是把Java对象(JavaBean)序列化为json格式字符串,将json格式字符 ...
- JSON解析实例——使用Json-lib
JSON解析实例——使用Json-lib Json-lib下载及使用 本文介绍用一个类库进行JSON解析. 工具下载地址:http://sourceforge.net/projects/json-li ...
随机推荐
- GC之九--gc调优
目标 满足应用的响应时间和吞吐量需求,尽量减少GC对应用的影响 原则 大部分时候都不需要调优GC,只需配置-Xms,-Xmx即可,JVM会自动进行调整 先满足响应时间需求,再满足吞吐量需求 FullG ...
- zabbix3.44+交换机华为或者H3C模版,监控所有的口updown以及流量的模版
https://files.cnblogs.com/files/itfat/zbx_export_templates.xml 直接在zabbix导入即可,华为和H3C oid在CPU和内存有少许区别. ...
- Warning: require(D:\wamp\www\glink-smart\bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in D:\wamp\www\glink-smart\bootstrap\autoload.php on line 1
Laravel访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire ...
- combiner中使用状态模式
mapreduce中的combine过程 hadoop的map过程执行完成后,每一个map都可能会产生大量的本地输出,Combiner的作用就是对map端的输出先做一次合并,减少在map和reduce ...
- Codeforces-20152016-northwestern-european-regional-contest-nwerc-A题
一.题目 二.题意 (1)一开始理解成:它最多需要开多少台电脑.同时,我又有个疑问,既然是最多需要开多少台,那不变成了总共有几个人开几台是最大的结果.然后,WA了无数发.直到比赛结束......其实说 ...
- js中的web加密
js中的web加密 window.crypto.subtle只会在安全模式下有用,也就是https环境下 创建摘要(硬解) var i = new TextEncoder('utf-8').encod ...
- java.lang.ClassFormatError: Extra bytes at the end of class file
在精简JRE过程中,将rt.jar中类通过FileInputStream,FileOutputStream进行拷贝操作出错: java.lang.ClassFormatError: Extra byt ...
- SDN openflow 学习小得
一.openflow 大概的工作原理 SDN 的一个大概简陋图, 同网段通讯 1.我们传统网络 pc1 10.1.1.1 要找同一子网的 pc2 10.1.1.2 通过广播洪泛.找到pc2,然后转发 ...
- Shiro异常处理总结
出自:https://blog.csdn.net/goodyuedandan/article/details/62420120 一.Spring MVC处理异常有3种方式: (1)使用Spring-M ...
- spring maven项目解决依赖jar包版本冲突方案
引入:http://blog.csdn.net/sanzhongguren/article/details/71191290 在spring reference中提到一个解决spring jar包之间 ...