[go]json序列化


// json字段别名
type User struct {
Id int `json:"id,string"`
Name string `json:"username"`
Age int `json:"age,omitempty"`
Address string `json:"-"`
}
// 字段别名
`json:"username"` // 得到的字段key为自定义的别名
`json:"id,string"` // 将int float bool 等以string类型序列化展示.
`json:"age,omitempty"` // 如果是零值,则序列化时忽略这个字段,结果字段不存在
`json:"-"` // 绝对忽略这个字段
// 使用性能更好的github.com/json-iterator/go包序列化
// 只需要给json重新赋值
package main
import (
"fmt"
"github.com/json-iterator/go"
)
type User struct {
Id int `json:"id,string"`
Name string `json:"username"`
Age int `json:"age,omitempty"`
Address string `json:"-"`
}
func main() {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
u := User{
Id: 12,
Name: "wendell",
Age: 1,
Address: "成都高新区",
}
data, err := json.Marshal(&u)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data))
//data:=[]byte(`{"id":"12","username":"wendell","age":1,"Address":"北京"}`)
//u2 := &User{}
//err = json.Unmarshal(data, u2)
//if err != nil {
// fmt.Println(err)
//}
//fmt.Printf("%+v \n", u2)
}
[go]json序列化的更多相关文章
- .Net深入实战系列—JSON序列化那点事儿
序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- DotNet的JSON序列化与反序列化
JSON(JavaScript Object Notation)JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.在现在的通信中,较多的采用JSON数据格式,JSON有 ...
- C#中JSON序列化和反序列化
有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境…… 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...
- 使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具
ASP.Net MVC默认的JSON序列化使用的是微软自己的JavaScriptSerializer.性能低不说,最让人受不了的是Dictionary<,>和Hashtable类型居然对应 ...
- Windows Phone 六、JSON序列化
JSON序列化 public class Person { public int Id { get; set; } public string Name { get; set; } public in ...
- [MVC_Json序列化]MVC之Json序列化循环引用
在做MVC项目时,难免会遇到Json序列化循环引用的问题,大致错误如下 错误1:序列化类型为“...”的对象时检测到循环引用. 错误2:Self referencing loop detected f ...
- NetworkComms V3 使用Json序列化器进行网络通信
刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...
- json序列化时datetime的处理方法
.net自带的json序列化器,JavaScriptSerializer和DataContractJsonSerializer,都是序列化成微软的datetime json格式,e.g. " ...
随机推荐
- js 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息
文件名的传递 ---全路径获取 $('#file').change(function(){ $('#em').text($('#file').val()); }); 文件名的传递 ---只获取文件名 ...
- MySQL 查询大于“时间字段”15分钟、1小时、1天的数据
以下代码中times为时间字段,类型为datetime 1.查询大于times十五分钟的数据 //大于号后面都是获取times十五分钟后的时间select*from table where now() ...
- element-ui Drawer抽屉组件封装
<template> <div class="com"> <el-drawer title="我是标题" :visible.syn ...
- Django中使用缓存
settings中的配置 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache ...
- 前端(HTML)+后端(Django)+数据库(MySQL):用户注册及登录演示
1.创建一个html文件用于简单的网页注册demo <!DOCTYPE html> <html lang="en"> <head> <me ...
- YOLO---YOLOv3 with OpenCV 再使用
YOLO---YOLOv3 with OpenCV 再使用YOLOv3 with OpenCV官网 @ https://github.com/JackKoLing/opencv_deeplearnin ...
- Robot Framework--修改log和报告的生成目录
1.修改log和报告的生成目录:-l F:\testreport\log -r F:\testreport\report -o F:\testreport\output -l:log -r:repor ...
- 关于静态资源放在CDN上
https://www.netlify.com/ https://app.netlify.com/signup?_ga=2.194141613.1097457726.1543799087-101005 ...
- Java8-LongAccumulator
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
- P4568 [JLOI2011]飞行路线 分层图最短路
思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...