当前位置:网站首页>[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"}
边栏推荐
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- Classic CTF topic about FTP protocol
- 【DesignMode】组合模式(composite mode)
- Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
- Notepad++ regular expression replacement string
- Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
- Basic introduction and source code analysis of webrtc threads
- Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
- Pointer - character pointer
- synchronized 和 ReentrantLock
猜你喜欢
Classic CTF topic about FTP protocol
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
How to solve the problems caused by the import process of ecology9.0
Idea remotely submits spark tasks to the yarn cluster
How to use the flutter framework to develop and run small programs
【DesignMode】装饰者模式(Decorator pattern)
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
Room cannot create an SQLite connection to verify the queries
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Browser local storage
随机推荐
[designmode] Decorator Pattern
Calculate sha256 value of data or file based on crypto++
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
notepad++正则表达式替换字符串
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
常用API类及异常体系
Browser local storage
Spark-SQL UDF函数
Key structure of ffmpeg - avframe
Arduino六足机器人
Date类中日期转成指定字符串出现的问题及解决方法
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
FPGA内部硬件结构与代码的关系
Spark SQL null value, Nan judgment and processing
Arduino hexapod robot
Notepad + + regular expression replace String
Solve the problem of reading Chinese garbled code in sqlserver connection database
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
XML Configuration File
SQLServer连接数据库读取中文乱码问题解决