当前位置:网站首页>[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
2022-07-06 00:35:00 【Programmer community】
List of articles
- One 、Groovy Object to json character string ( Use JsonBuilder convert )
- Two 、 Use JsonOutput Convert an object of the specified type to json character string
- 3、 ... and 、 take json String format output
- Four 、 Complete code example
One 、Groovy Object to json character string ( Use JsonBuilder convert )
Statement Student class , Declare in it
2
2
2 Members , name
and age
;
class Student {
def name def age}
establish Student Object time , Assign values to these two members in the constructor
def student = new Student(name: "Tom", age: 18)
establish json generator JsonBuilder object , Constructor passed in Student object , Can finish json transformation , take Student The object is changed to json character string ;
// json generator def jsonBuilder = new JsonBuilder(student)println jsonBuilder.toString()
Code example :
import groovy.json.JsonBuilderclass Student {
def name def age}def student = new Student(name: "Tom", age: 18)// json generator def jsonBuilder = new JsonBuilder(student)println jsonBuilder.toString()
Execution results :
{
"age":18,"name":"Tom"}
Two 、 Use JsonOutput Convert an object of the specified type to json character string
JsonOutput Can be Map , URL , String , Number , Date , UUID , Boolean And other types of objects are converted to json character string ;
take Student Object to json The code is as follows :
// take Student Object to jsondef json = JsonOutput.toJson(student)println json
Execution results :
{
"age":18,"name":"Tom"}
3、 ... and 、 take json String format output
Use JsonOutput.prettyPrint(json)
Can be json Format the output ,
The function prototype is as follows :
/** * Pretty print a JSON payload. * * @param jsonPayload * @return a pretty representation of JSON payload. */ public static String prettyPrint(String jsonPayload) {
}
take {"age":18,"name":"Tom"}
Use the format output above ,
// Format output json data println JsonOutput.prettyPrint(json)
Output results :
{
"age": 18, "name": "Tom"}
Four 、 Complete code example
Complete code example :
import groovy.json.JsonBuilderimport groovy.json.JsonOutputclass Student {
def name def age}def student = new Student(name: "Tom", age: 18)// json generator def jsonBuilder = new JsonBuilder(student)println jsonBuilder.toString()// take Student Object to jsondef json = JsonOutput.toJson(student)println json// Format output json data println JsonOutput.prettyPrint(json)
Execution results :
{
"age":18,"name":"Tom"}{
"age":18,"name":"Tom"}{
"age": 18, "name": "Tom"}
边栏推荐
- OpenCV经典100题
- Search (DFS and BFS)
- devkit入门
- STM32按键消抖——入门状态机思维
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- STM32 configuration after chip replacement and possible errors
- Browser local storage
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- 7.5 simulation summary
- JS can really prohibit constant modification this time!
猜你喜欢
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
The relationship between FPGA internal hardware structure and code
Browser local storage
如何利用Flutter框架开发运行小程序
MySQL storage engine
关于slmgr命令的那些事
2022-02-13 work record -- PHP parsing rich text
如何解决ecology9.0执行导入流程流程产生的问题
AtCoder Beginner Contest 258【比赛记录】
Room cannot create an SQLite connection to verify the queries
随机推荐
Key structure of ffmpeg -- AVCodecContext
Introduction of motor
How to make your own robot
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Uniapp development, packaged as H5 and deployed to the server
剖面测量之提取剖面数据
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
STM32 configuration after chip replacement and possible errors
Hudi of data Lake (2): Hudi compilation
Notepad + + regular expression replace String
Knowledge about the memory size occupied by the structure
Codeforces round 804 (Div. 2) [competition record]
XML Configuration File
FFT learning notes (I think it is detailed)
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
OpenCV经典100题
Room cannot create an SQLite connection to verify the queries
Key structure of ffmpeg - avformatcontext
【DesignMode】适配器模式(adapter pattern)
Solve the problem of reading Chinese garbled code in sqlserver connection database