golang的json操作[转]
package mainimport ( "encoding/json" "fmt" "os")type ConfigStruct struct { Host string `json:"host"` Port int `json:"port"` AnalyticsFile string `json:"analytics_file"` StaticFileVersion int `json:"static_file_version"` StaticDir string `json:"static_dir"` TemplatesDir string `json:"templates_dir"` SerTcpSocketHost string `json:"serTcpSocketHost"` SerTcpSocketPort int `json:"serTcpSocketPort"` Fruits []string `json:"fruits"`}type Other struct { SerTcpSocketHost string `json:"serTcpSocketHost"` SerTcpSocketPort int `json:"serTcpSocketPort"` Fruits []string `json:"fruits"`}func main() { jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple", "peach"]}` //json str 转map var dat map[string]interface{} if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil { fmt.Println("==============json str 转map=======================") fmt.Println(dat) fmt.Println(dat["host"]) } //json str 转struct var config ConfigStruct if err := json.Unmarshal([]byte(jsonStr), &config); err == nil { fmt.Println("================json str 转struct==") fmt.Println(config) fmt.Println(config.Host) } //json str 转struct(部份字段) var part Other if err := json.Unmarshal([]byte(jsonStr), &part); err == nil { fmt.Println("================json str 转struct==") fmt.Println(part) fmt.Println(part.SerTcpSocketPort) } //struct 到json str if b, err := json.Marshal(config); err == nil { fmt.Println("================struct 到json str==") fmt.Println(string(b)) } //map 到json str fmt.Println("================map 到json str=====================") enc := json.NewEncoder(os.Stdout) enc.Encode(dat) //array 到 json str arr := []string{"hello", "apple", "python", "golang", "base", "peach", "pear"} lang, err := json.Marshal(arr) if err == nil { fmt.Println("================array 到 json str==") fmt.Println(string(lang)) } //json 到 []string var wo []string if err := json.Unmarshal(lang, &wo); err == nil { fmt.Println("================json 到 []string==") fmt.Println(wo) }} |
from:http://my.oschina.net/golang/blog/210012
golang的json操作[转]的更多相关文章
- golang的json操作
package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct s ...
- 48 【golang】json的效率
本文将主要做如下几方面的测试: 1,构造一个[100]struct的数组,然后来测试它的json编码后的字符串 或者([]byte),首先关心它的功能是否正常: 2,在很早之前,我们在使用golang ...
- Golang 处理 Json(二):解码
golang 编码 json 还比较简单,而解析 json 则非常蛋疼.不像 PHP 一句 json_decode() 就能搞定.之前项目开发中,为了兼容不同客户端的需求,请求的 content-ty ...
- Golang的json包
encoding/json encoding/json是官方提供的标准json, 实现RFC 7159中定义的JSON编码和解码.使用的时候需要预定义struct,原理是通过reflection和in ...
- Golang解析json的几种方法
Golang解析json的几种方法 概要 使用Golang调用其它平台API接口时总会被多层的json串给恶心到,我记录一下自己解析json的几种方法. 一.自带的json包 func JsonUnm ...
- Json操作(DynamicJson)
Json的简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了 ...
- ASP.NET 里的 JSON操作
最近项目中需要用到 JSON操作,google了一下 找到了几个比较好的操作方法.... 一 .使用 mircosoft 提供的 .NET Framework 自带的 json操作方法 1. 使用Ja ...
- Newtonsoft.Json 操作 JSON 字符串
Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataContractJsonSeriali ...
- Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类
本文目录 1. 前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...
随机推荐
- LitJson JavaScriptSerializer
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 流和Variant的转换
procedure TForm2.VariantToStream (const v : olevariant; Stream : T ...
- scala -- 层级
层级 层级的顶端是Any 类,定义了如下方法 final def ==(that:Any):Boolean final def !=(that:Any):Boolean def equals(that ...
- [故障引起的故事]URL中带加号的处理
问题起因: 客户订购了一关键字为"e+h 变送器" , 在首页推荐广告中,会根据用户在search 搜索过的关键字进行一个匹配投放.技术实现是UED 通过JS 获取coo ...
- Linux运维就业技术指导(九)期末架构考核
一,毕业架构设计考核筹备 1.1,架构图模板示例 1.1.1 架构图(一)概述 本架构是4层lvs负载均衡给后方7层nginx反向代理: 业务进行了动静分离: 数据库前端有memcached缓存组,降 ...
- 安装Anaconda以及jupyter的使用
1)下载https://www.anaconda.com/download/ 2)安装 3)终端查看(Anaconda Prompt) 4)升级所有的包 初次安装下的软件包版本一般都比较老旧,因此提前 ...
- iperf——网络性能测试工具
一.前言 工作中遇到需要测试Linux服务器网卡占用率的场景,查阅资料后,发现iperf是一款合适的网络测速工具. 下面讲解一下如何使用iperf做网络性能测试. 二.基础知识 先补充一些基础知识: ...
- ReactNative手势解锁(react-native-ok-gesture-password)
在大前端的趋势之下,我也慢慢开始从事React Native相关的开发.但是奈何React Native生态相对于Android来说还是太小了.许多开源的库早早就已经不再维护.之前项目中需要用到手势解 ...
- tag-SMASS-1
SMASS 是在vasp的DFTMD中决定着系综的类型,在手册中给出的该参数具体信息如下: SMASS = -3 | -2 | -1 | [real] ≥ 0 Default: SMASS = -3 ...
- 【校招面试 之 C/C++】第20题 C++ STL(二)之Vector
1.vector的动态增长 当添加元素时,如果vector空间大小不足,则会以原大小的两倍另外配置一块较大的新空间,然后将原空间内容拷贝过来,在新空间的内容末尾添加元素,并释放原空间.vector的空 ...