由于前台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序列化对象时,非法字符错误原因的更多相关文章

  1. EF中Json序列化对象时检测到循环引用的解决办法

    MVC4 EF中将数据表外键引用的是自身,转换成Json时,总是提示错误:“序列化类型为....的对象时检测到循环引用.”: 解决办法: 把要序列化的对象转为匿名对象去掉导航属性,如下 :本来是var ...

  2. 使用DataContractJsonSerializer发序列化对象时出现的异常

    最近服务器上的某个程序的错误日志中频繁出现以下异常: Deserialising: There was an error deserializing the object of type {type} ...

  3. .NET 自定义Json序列化时间格式

    .NET 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规 ...

  4. JSON.Net 自定义Json序列化时间格式

    JSON.Net 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认 ...

  5. Error serializing object:序列化对象时出错

    序列化对象时出错 :Error serializing object. Error serializing object. Cause: java.io.NotSerializableExceptio ...

  6. 【翻译自mos文章】使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式。

    使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式. 參考原文: ORA-01555 Using Automatic Undo M ...

  7. SpringBoot之解决一对一、多对一、多对多等关联实体在JSON序列化/输出时产生的无限递归死循环问题(infinite recursion)

    前言 这问题着实让人苦不堪言,有必要把它记下了. @JsonBackReference [亲测有效] 1.使用注解@JsonBackReference标记在有关联关系的实体属性上 2.仅导入此注解类有 ...

  8. Json序列化对象

    之前都是用的二进制的序列化方法,是.net自带的,但是最常用到的还是Json序列化 (1)只需要调用 Newtonsoft.Json.dll 即可 public class JsonTools { / ...

  9. Gson序列化对象时排除字段

    import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; /** *Gson序列化对象排除属性 ...

随机推荐

  1. JS 对应CSS 样式

    首先,把CSS和JS标签style属性对照表了解了: CSS 和 JavaScript 标签 style 属性对照表: 盒子标签和属性对照 颜色和背景标签和属性对照 样式标签和属性对照 文字样式标签和 ...

  2. 【转】 oracle 层次查询判断叶子和根节点

    Oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下: DROP TABLE idb_hierarchical; create TABLE idb_hierarchical ...

  3. 设置定时任务(Timer类的介绍)

    设置定时任务(Timer类的介绍) 在我们的很多项目中,我们都须要用到定时任务,因此想借此博文来对定时任务进行一个介绍. 设置定时任务过程例如以下: 先new一个Timer对象 Timer timer ...

  4. sparkonhbase

    import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.client.Result impor ...

  5. win 安装pip和requests

    下载地址是:https://pypi.python.org/pypi/pip#downloads : 下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入: python setup.py ...

  6. am335x Lan8710a 双网口配置

    一. 经过调试, LAN8710A在 am335x 上面需要使用 GMII的模式,设备树 pin mux配置如下: // 下面是工作模式的配置,在睡眠模式下是配成GPIO模式 162 cpsw_def ...

  7. Ext.ux.grid.feature.Searching 解析查询参数,动态产生linq lambda表达式

    上篇文章中http://www.cnblogs.com/qidian10/p/3209439.html我们介绍了如何使用Grid的查询组建,而且将查询的参数传递到了后台. 那么我们后台如何介绍参数,并 ...

  8. Java学习笔记:具体解释传值和传引用

    传值和传引用 When you're passing primitives into a method ,you get a distinct copy of the primitive. When ...

  9. Nodejs中export的作用

    在上一节,我们编写了一个hello.js文件,这个hello.js文件就是一个模块,模块的名字就是文件名(去掉.js后缀),所以hello.js文件就是名为hello的模块. 我们把hello.js改 ...

  10. Nagios系列1,选择

    Zabbix和Nagios哪个更好 zabbix: 1.分布式监控,适合于构建分布式监控系统,具有node,proxy 2种分布式模式 2.自动化功能,自动发现,自动注册主机,自动添加模板,自动添加分 ...