当前位置:网站首页>[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"}
边栏推荐
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- Promise
- 如何解决ecology9.0执行导入流程流程产生的问题
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- Spark SQL空值Null,NaN判断和处理
- NLP generation model 2017: Why are those in transformer
- 数据分析思维分析方法和业务知识——分析方法(二)
- [designmode] adapter pattern
- Idea remotely submits spark tasks to the yarn cluster
- LeetCode 6006. Take out the least number of magic beans
猜你喜欢
Ffmpeg learning - core module
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
MySQL functions
Knowledge about the memory size occupied by the structure
Idea remotely submits spark tasks to the yarn cluster
Go learning - dependency injection
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
Go learning --- structure to map[string]interface{}
Room cannot create an SQLite connection to verify the queries
随机推荐
Classical concurrency problem: the dining problem of philosophers
Idea远程提交spark任务到yarn集群
Uniapp development, packaged as H5 and deployed to the server
电机的简介
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Key structure of ffmpeg - avframe
Promise
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
如何解决ecology9.0执行导入流程流程产生的问题
[designmode] composite mode
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
Hudi of data Lake (1): introduction to Hudi
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Yolov5, pychar, Anaconda environment installation
About the slmgr command
Codeforces round 804 (Div. 2) [competition record]
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers