【玩转Golang】 自定义json序列化对象时,非法字符错误原因
由于前台web页面传来的日期对象是这样的格式“2010-11-03 15:23:22”,所以我安装网上查来的办法,自定义包装了time.Time对象,实现自己的Marshal和UnMarshal方法
type DateTime struct {
time.Time
}
const ctLayout = "2006-01-02 15:04:05"
const ctLayout_nosec = "2006-01-02 15:04"
const ctLayout_date = "2006-01-02"
func (this *DateTime) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
sv := string(b)
if len(sv) == 10 {
sv += " 00:00:00"
} else if len(sv) == 16 {
sv += ":00"
}
this.Time, err = time.ParseInLocation(ctLayout, string(b), loc)
if err != nil {
if this.Time, err = time.ParseInLocation(ctLayout_nosec, string(b), loc); err != nil {
this.Time, err = time.ParseInLocation(ctLayout_date, string(b), loc)
}
}
return
}
func (this *DateTime) MarshalJSON() ([]byte, error) {
rs := []byte(this.Time.Format(ctLayout))
return rs, nil
}
var nilTime = (time.Time{}).UnixNano()
func (this *DateTime) IsSet() bool {
return this.UnixNano() != nilTime
}
然后,把结构中声明为time.Time的都修改为自定义的类型DateTime,试了一下,发现已经可以正确解析网页发来的时间,但是在输出时,总是不对,好像并没有调用自定义的Marshal方法。编写测试方法发现,原来json.Marshal方法调用DateTime.Marshal时出错了!
invalid character '-' after top-level value
开始没有认真看,以为“-”号不合法,就换了一个,结果错误一样:
invalid character '/' after top-level value
看来,根本不是分割符的问题,仔细分析错误,发现“top-level”字样,我这返回的就是一个字符串,怎么可能top-level呢!想到这儿突然醒悟,是不是返回字符串应该自己加引号呢,急忙修改代码一试,果然!~_~
最终代码如下:
type DateTime struct {
time.Time
}
const ctLayout = "2006-01-02 15:04:05"
const ctLayout_nosec = "2006-01-02 15:04"
const ctLayout_date = "2006-01-02"
func (this *DateTime) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
sv := string(b)
if len(sv) == 10 {
sv += " 00:00:00"
} else if len(sv) == 16 {
sv += ":00"
}
this.Time, err = time.ParseInLocation(ctLayout, string(b), loc)
if err != nil {
if this.Time, err = time.ParseInLocation(ctLayout_nosec, string(b), loc); err != nil {
this.Time, err = time.ParseInLocation(ctLayout_date, string(b), loc)
}
}
return
}
func (this *DateTime) MarshalJSON() ([]byte, error) {
rs := []byte(fmt.Sprintf(`"%s"`, this.Time.Format(ctLayout)))
return rs, nil
}
var nilTime = (time.Time{}).UnixNano()
func (this *DateTime) IsSet() bool {
return this.UnixNano() != nilTime
}
【玩转Golang】 自定义json序列化对象时,非法字符错误原因的更多相关文章
- EF中Json序列化对象时检测到循环引用的解决办法
MVC4 EF中将数据表外键引用的是自身,转换成Json时,总是提示错误:“序列化类型为....的对象时检测到循环引用.”: 解决办法: 把要序列化的对象转为匿名对象去掉导航属性,如下 :本来是var ...
- 使用DataContractJsonSerializer发序列化对象时出现的异常
最近服务器上的某个程序的错误日志中频繁出现以下异常: Deserialising: There was an error deserializing the object of type {type} ...
- .NET 自定义Json序列化时间格式
.NET 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规 ...
- JSON.Net 自定义Json序列化时间格式
JSON.Net 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认 ...
- Error serializing object:序列化对象时出错
序列化对象时出错 :Error serializing object. Error serializing object. Cause: java.io.NotSerializableExceptio ...
- 【翻译自mos文章】使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式。
使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式. 參考原文: ORA-01555 Using Automatic Undo M ...
- SpringBoot之解决一对一、多对一、多对多等关联实体在JSON序列化/输出时产生的无限递归死循环问题(infinite recursion)
前言 这问题着实让人苦不堪言,有必要把它记下了. @JsonBackReference [亲测有效] 1.使用注解@JsonBackReference标记在有关联关系的实体属性上 2.仅导入此注解类有 ...
- Json序列化对象
之前都是用的二进制的序列化方法,是.net自带的,但是最常用到的还是Json序列化 (1)只需要调用 Newtonsoft.Json.dll 即可 public class JsonTools { / ...
- Gson序列化对象时排除字段
import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; /** *Gson序列化对象排除属性 ...
随机推荐
- Sublime Text 3安装与使用,安装插件,快捷键,默认配置
本文是Sublime Text 全程指引 by Lucida (http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html)的笔 ...
- wcf中的Message类
客户端->服务端—>客户端 客户端代码: using (new OperationContextScope(client.InnerChannel)) { ...
- spring mvc之启动过程源码分析
简介 这两个星期都在看spring mvc源码,看来看去还是还是很多细节没了解清楚,在这里把看明白的记录下,欢迎在评论中一起讨论. 一.铺垫 spring mvc是基于servlet的,在正式分析之前 ...
- [echo]echo输出换行
echo -e "hello\nworld", -e处理转义字符
- IoCopyCurrentIrpStackLocationToNext与IoSetCompletionRoutine的深入理解
1.IoCopyCurrentIrpStackLocationToNext是拷贝本层的IO_STACK_LOCATION 到下一层.在楚狂人的驱动教程中说:如果对irp完成之后的事情有兴趣,并打算在完 ...
- 简单的Http请求数据保存到Hdfs
使用okhttp工具集来开发:(如果文件已经存在会报错) package com.etl; import java.io.IOException; import org.apache.commons. ...
- Linux/Unix下的任务管理器-top命令
Windows下的任务管理器虽然不好用(个人更喜欢Process Explorer些),但也算方便,可以方便的查看进程,CPU,内存...也可以很容易的结束进程 没有图形化界面下的Linux,也有命令 ...
- mysql show global variables字符超1024会被截断
show variables 会存在数据被截断的问题: select 全局变量没有问题 官网解释:https://dev.mysql.com/doc/refman/5.6/en/variables-t ...
- Php5.5新特性 Generators详解
在PHP5.5.0版本中,新增了生成器(Generators)特性,用于简化实现迭代器接口(Iterator)创建简单的迭代器的复杂性. 通过生成器,我们可以轻松的使用foreach迭代一系列的数据, ...
- Excel相同内容如何设置相同的背景色
有这样一个需求就是实现EXCEL的相同内容的背景色相同.并且内容不同的时候达到隔行变色的效果,记录下实现的效果,如果大家有什么更好的办法请给我指点一下.具体操作如下: 首先将是比较的列"20 ...