当前位置:网站首页>[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"}
边栏推荐
- Cloud guide DNS, knowledge popularization and classroom notes
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- How to solve the problems caused by the import process of ecology9.0
- Spark DF增加一列
- devkit入门
- FFmpeg抓取RTSP图像进行图像分析
- Room cannot create an SQLite connection to verify the queries
- LeetCode 斐波那契序列
- Reading notes of the beauty of programming
- Spark AQE
猜你喜欢

Set data real-time update during MDK debug

Room cannot create an SQLite connection to verify the queries

FFT learning notes (I think it is detailed)

OS i/o devices and device controllers

Classic CTF topic about FTP protocol

Leetcode:20220213 week race (less bugs, top 10% 555)

【DesignMode】组合模式(composite mode)

Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4

Knowledge about the memory size occupied by the structure

Problems and solutions of converting date into specified string in date class
随机推荐
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
KDD 2022 | 脑电AI助力癫痫疾病诊断
Analysis of the combination of small program technology advantages and industrial Internet
Notepad + + regular expression replace String
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
Leetcode 450 deleting nodes in a binary search tree
【线上小工具】开发过程中会用到的线上小工具合集
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
PHP determines whether an array contains the value of another array
synchronized 和 ReentrantLock
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Idea远程提交spark任务到yarn集群
OpenCV经典100题
STM32 configuration after chip replacement and possible errors
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
Room cannot create an SQLite connection to verify the queries
Promise
7.5 simulation summary
What is information security? What is included? What is the difference with network security?
Spark DF增加一列