首页 > 其他分享 >Go - Creating JSON Data Byte Arrays from Structs

Go - Creating JSON Data Byte Arrays from Structs

时间:2023-10-03 11:55:44浏览次数:34  
标签:Creating err Arrays json person JSON want data

Problem: You want to create JSON data from a struct.


Solution: Create the structs then use the json.Marshal or json.MarshalIndent to marshal the data into a JSON slice of bytes.

 

func   main ()   { 
      person   :=   struct{}

      data ,   err   :=   json . Marshal ( & person ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  marshal  person:" ,   err ) 
      } 
      err   =   os . WriteFile ( "han.json" ,   data ,   0644 ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  write  to  file" ,   err ) 
      } 
}

The file is not very readable. If you want a more readable version, you can use json.Mar⁠shalIndent instead. You need to put in two more parameters: the first is the prefix, and the second is the indent. Mostly if you want to have a clean JSON output, the prefix is an empty string while the indent is a single space:

data ,   err   :=   json . MarshalIndent ( & person ,   "" ,   "  " )

 

标签:Creating,err,Arrays,json,person,JSON,want,data
From: https://www.cnblogs.com/zhangzhihui/p/17740931.html

相关文章

  • Go - Parsing JSON Data Streams Into Structs
    Problem: YouwanttoparseJSONdatafromastream.Solution: CreatestructstocontaintheJSONdata.CreateadecoderusingNewDecoderintheencoding/jsonpackage,thencallDecodeonthedecodertodecodedataintothestructs. InthisfirstJSONf......
  • Arrays常用方法
    1.Arrays.toString()方法方法作用:快速输出数组内容int[]a={1,2,3,4,5};System.out.println(Arrays.toString(a));//输出格式:[1,2,3,4,5]2.Arrays.sort()方法方法作用:给数组排序,默认升序int[]a=newint[5]{5,4,3,2,1};Arrays.sort(a);//12345System.out.printl......
  • JSON基础
    概述JavaScriptObjectNotation(JavaScript对象表示法)简称JSON是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理......
  • 前端JSON.stringify,JSON.parse函数
    JSON.stringify将对象转为JSON字符串;JSON.parse将JSON字符串转为对象;对象:{productId:129}JSON字符串:"{\"productId\":129}"***JSON使用场景***1. localStorage/sessionStorage存储对象  localStorage/sessionStorage只可以存储字符串,当我们想存储对象的时候,需要使用JSON.s......
  • Java Arrays.fill() 方法详解
    在Java编程中,数组是一个非常常见的数据结构,而Java提供了许多有用的数组操作方法来简化开发过程。其中之一是Arrays.fill()方法,它允许我们填充一个数组的所有元素,将它们设置为指定的值。在本篇文章中,我们将深入探讨Arrays.fill()方法的用法、参数和示例,以帮助您更好地理解和使用它。......
  • 爬取豆瓣电影,保存到json文件中
    importurllib.requesturl='https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537......
  • Go每日一库之186:sonic(高性能JSON库)
    介绍我们在日常开发中,常常会对JSON进行序列化和反序列化。Golang提供了encoding/json包对JSON进行Marshal/Unmarshal操作。但是在大规模数据场景下,该包的性能和开销确实会有点不够看。在生产环境下,JSON序列化和反序列化会被频繁的使用到。在测试中,CPU使用率接近10%,其中极端情况......
  • Go - Creating Customized Errors
    Problem: Youwanttocreatecustomerrorstocommunicatemoreinformationabouttheerrorencountered.Solution: Createanewstring-basederrororimplementtheerrorinterfacebycreatingastructwithanErrormethodthatreturnsastring. Therea......
  • Mac部署Python语言json模块(Anaconda)
      本文介绍在Mac电脑的Anaconda环境中,配置Python语言中,用以编码、解码、处理JSON数据的json库的方法;在Windows电脑中配置json库的方法也是类似的,大家可以一并参考。  JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,常用于数据的序列化和传输。而Python中的json库,......
  • 对象转JSON 遇到的BigDecimal 科学计数法的问题,json转化字段单独处理
    问题描述:项目需要发送JSON数据,BigDecimal转成json仍然显示科学计数法,如果使用BigDecimai的toPlainString()需要将数据格式转为String,所以找了一下fastjson的自定义序列化内容,记录一下,以免以后忘记解决方案:方案一:JSONObject.toJSONString(vo,SerializerFeature.WriteBigDecimalA......