<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Json on Mr.Yu-技术笔记</title>
    <link>https://yujinping.top/tags/json/</link>
    <description>Recent content in Json on Mr.Yu-技术笔记</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>zh-CN</language>
    <lastBuildDate>Sat, 09 Apr 2022 13:05:00 +0800</lastBuildDate>
    <atom:link href="https://yujinping.top/tags/json/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>go web 接口返回time.Time类型数据的格式化</title>
      <link>https://yujinping.top/post/go/go_time_json_format/</link>
      <pubDate>Sat, 09 Apr 2022 13:05:00 +0800</pubDate>
      <guid>https://yujinping.top/post/go/go_time_json_format/</guid>
      <description>使用自定义类型并重写 JSON 序列化和反序列化的方法 Go Web 接口结构体 type User struct { Id uint64 `json:&amp;#34;id&amp;#34;` Username string `json:&amp;#34;username&amp;#34;` Password string `json:&amp;#34;password&amp;#34;` CreateTime time.Time `json:&amp;#34;createTime&amp;#34;` }接口输出 createTime 字段的时候其格式并不符合我们的习惯。可以使用类型别名的方式重写 JSON 的两个方法： MarshalJSON() ([]byte, error) UnmarshalJSON(data []byte) error package types import ( &amp;#34;fmt&amp;#34; &amp;#34;time&amp;#34; ) type DateTime time.Time func (d DateTime) MarshalJSON() ([]byte, error) { // 重写time转换成json之后的格式 var str = fmt.Sprintf(&amp;#34;\&amp;#34;%s\&amp;#34;&amp;#34;, time.Time(d).Format(&amp;#34;2006-01-02T15:04:05&amp;#34;)) return []byte(str), nil }</description>
    </item>
  </channel>
</rss>
