当前位置:网站首页>[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
2022-07-06 00:34:00 【Programmer community】
List of articles
- One 、 Use JsonSlurper Conduct json String deserialization
- Two 、 according to map Collection construction related classes
- 3、 ... and 、 Complete code example
One 、 Use JsonSlurper Conduct json String deserialization
Will be as follows json character string
{
"age":18,"name":"Tom"}deserialize ;
First , establish JsonSlurper object ,
def jsonSlurper = new JsonSlurper()then , Call the JsonSlurper Object's parse Method , Different types of json Data is deserialized ;
Deserializable json The data type is shown in the figure below :

This call jsonSlurper Object's parseText Method , take json Deserialize the string ;
// String json Deserialization operation , obtain map aggregate def jsonObject = jsonSlurper.parseText('{"age":18,"name":"Tom"}');Print the deserialization result ,
// Print the deserialization result println jsonObjectThe results are as follows :
[age:18, name:Tom]Two 、 according to map Collection construction related classes
In the above chapter json character string
{
"age":18,"name":"Tom"}deserialize , obtain
[age:18, name:Tom]map aggregate ;
The map The set can be directly assigned to the corresponding class object , The variable name of this class is map A collection of Key key ;
// according to map A collection of Key Construction class class Student {
def name def age}Direct will map Set is assigned to the constructed class object , And print the member variable value of the object ;
// Direct will map Set is assigned to the constructed class object Student student = jsonObject// Print the value of the member variable of the class object println "${student.name} , ${student.age}"The execution result is :
Tom , 183、 ... and 、 Complete code example
Complete code example :
import groovy.json.JsonSlurper// take {"age":18,"name":"Tom"} String is deserialized def jsonSlurper = new JsonSlurper()// String json Deserialization operation , obtain map aggregate def jsonObject = jsonSlurper.parseText('{"age":18,"name":"Tom"}');// Print the deserialization result println jsonObject// according to map A collection of Key Construction class class Student {
def name def age}// Direct will map Set is assigned to the constructed class object Student student = jsonObject// Print the value of the member variable of the class object println "${student.name} , ${student.age}"Execution results :
[age:18, name:Tom]Tom , 18
边栏推荐
- Comment faire votre propre robot
- Browser reflow and redraw
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- XML Configuration File
- Go learning --- read INI file
- How to solve the problems caused by the import process of ecology9.0
- Pointer pointer array, array pointer
- 【DesignMode】装饰者模式(Decorator pattern)
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- Spark DF增加一列
猜你喜欢

Permission problem: source bash_ profile permission denied

2022-02-13 work record -- PHP parsing rich text
![[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)](/img/39/9d189a18f3f75110b400506e274391.png)
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)

图解网络:TCP三次握手背后的原理,为啥两次握手不可以?

uniapp开发,打包成H5部署到服务器

Data analysis thinking analysis methods and business knowledge - analysis methods (III)

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

Common API classes and exception systems
![[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)](/img/d6/c3128e26d7e629b7f128c551cd03a7.png)
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)

Folding and sinking sand -- weekly record of ETF
随机推荐
Room cannot create an SQLite connection to verify the queries
Go learning --- structure to map[string]interface{}
Single source shortest path exercise (I)
看抖音直播Beyond演唱会有感
电机的简介
XML配置文件
免费的聊天机器人API
关于slmgr命令的那些事
LeetCode 6006. Take out the least number of magic beans
Yolov5、Pycharm、Anaconda环境安装
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
Spark-SQL UDF函数
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
AtCoder Beginner Contest 254【VP记录】
如何利用Flutter框架开发运行小程序
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
Codeforces round 804 (Div. 2) [competition record]
LeetCode 斐波那契序列
Opencv classic 100 questions
What is information security? What is included? What is the difference with network security?