当前位置:网站首页>[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 jsonExecution 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"}
边栏推荐
- Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
- Ffmpeg learning - core module
- Single source shortest path exercise (I)
- Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
- How spark gets columns in dataframe --column, $, column, apply
- Cloud guide DNS, knowledge popularization and classroom notes
- uniapp开发,打包成H5部署到服务器
- Spark-SQL UDF函数
- AtCoder Beginner Contest 254【VP记录】
- MySQL存储引擎
猜你喜欢

剖面测量之提取剖面数据

Spark SQL null value, Nan judgment and processing

Leetcode 450 deleting nodes in a binary search tree

LeetCode 1598. Folder operation log collector

如何利用Flutter框架开发运行小程序

Idea remotely submits spark tasks to the yarn cluster

notepad++正则表达式替换字符串

anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败

小程序技术优势与产业互联网相结合的分析

wx. Getlocation (object object) application method, latest version
随机推荐
7.5 decorator
[designmode] adapter pattern
[simple implementation of file IO]
wx. Getlocation (object object) application method, latest version
Gd32f4xx UIP protocol stack migration record
Leetcode Fibonacci sequence
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
XML配置文件
Atcoder beginer contest 258 [competition record]
Yolov5、Pycharm、Anaconda环境安装
Spark AQE
Key structure of ffmpeg - avframe
Calculate sha256 value of data or file based on crypto++
LeetCode 1189. Maximum number of "balloons"
【线上小工具】开发过程中会用到的线上小工具合集
FFmpeg抓取RTSP图像进行图像分析
Spark DF adds a column
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
AtCoder Beginner Contest 258【比赛记录】