一个关于json.Marshal 返回值, 使用fmt.Println输出,屏幕输出却不是json格式的问题

  1. 将[]map[string]interface{} 使用json.Marshal进行序列化,将返回结果使用fmt.Println 进行输出,数据结果不是json内容。
  • demo code:

    jsonStr := `[{"mid":"4686371941518476","begin":"1632887992","image":"https://wx2.sinaimg.cn/mw690/006KoMX5ly1guxcwyj9vnj603c03cjr902.jpg","aid":"6616523296","tags":"1042015:newTagCategory_004|1042015:newAbilityTag_004005|1042015:city_14005176","level":"S","expired":"1632974393","text":"\u963f\u5bcc\u6c57\u7684\u672a\u6765\u5982\u4f55","event_id":"10000040","data_type":"0","source":"operation_hot"}]`
      m := make([]map[string]interface{}, 0)
      err = json.Unmarshal([]byte(str), &m)
      if err != nil {
          panic(err)
      }
      b, err := json.Marshal(m)
      if err != nil {
          panic(err)
      }
      fmt.Println("", string(b))     // 1. 输出结果正确,json内容
      fmt.Println(string(b))         // 2. 输出结果错误,非json内容
    
      // 屏幕输出:
      // 1.[{"aid":"6616523296","begin":"1632887992","data_type":"0","event_id":"10000040","expired":"1632974393","image":"https://wx2.sinaimg.cn/mw690/006KoMX5ly1guxcwyj9vnj603c03cjr902.jpg","level":"S","mid":"4686371941518476","source":"operation_hot","tags":"1042015:newTagCategory_004|1042015:newAbilityTag_004005|1042015:city_14005176","text":"阿富汗的未来如何"}]
       // 2. [{"aid":"6616523296","begin":"1632887992","data_type":"0","event_id":"10000040","expired":"1632974393","image":"https://wx2.sinaimg.cn/mw690/006KoMX5ly1guxcwyj9vnj603c03cjr902.jpg","level":"S","mid":"4686371941518476","source":"operation_hot","tags":"1042015:newTagCategory_004|1042015:newAbilityTag_004005|1042015:city_14005176","text":"阿富汗的未]  -->非json
  1. 查看了fmt.Println 输出代码,区别只是在写buf时多写入一个空格,输出却有差异,不太清楚具体原因
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
   p := newPrinter()
   p.doPrintln(a)
   n, err = w.Write(p.buf)
   p.free()
   return
}

func (p *pp) doPrintln(a []interface{}) {
    for argNum, arg := range a { 
        //这里只是多写入了一个空格
        if argNum > 0 {
            p.buf.writeByte(' ')
        }
        p.printArg(arg, 'v')
    }
    p.buf.writeByte('\n')
}
附言 1  ·  2年前

已解决问题,是我使用的goland ide的设置问题,目前还不清楚哪里设置不对,在shell下运行是没问题的

讨论数量: 1
fmt.Println("", string(b))
fmt.Println(string(b))
 [{"aid":"6616523296","begin":"1632887992","data_type":"0","event_id":"10000040","expired":"1632974393","image":"https://wx2.sinaimg.cn/mw690/006KoMX5ly1guxcwyj9vnj603c03cjr902.jpg","level":"S","mid":"4686371941518476","source":"operation_hot","tags":"1042015:newTagCategory_004|1042015:newAbilityTag_004005|1042015:city_14005176","text":"阿富汗的未来如何"}]
[{"aid":"6616523296","begin":"1632887992","data_type":"0","event_id":"10000040","expired":"1632974393","image":"https://wx2.sinaimg.cn/mw690/006KoMX5ly1guxcwyj9vnj603c03cjr902.jpg","level":"S","mid":"4686371941518476","source":"operation_hot","tags":"1042015:newTagCategory_004|1042015:newAbilityTag_004005|1042015:city_14005176","text":"阿富汗的未来如何"}]
成功: 进程退出代码 0.

我电脑上输出是正常的,Go版本1.15

2年前 评论
CodePorter (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!