当前位置:网站首页>[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 jsonObject
The 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 , 18
3、 ... 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
边栏推荐
- Codeforces Round #804 (Div. 2)【比赛记录】
- [Online gadgets] a collection of online gadgets that will be used in the development process
- 电机的简介
- Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
- Analysis of the combination of small program technology advantages and industrial Internet
- Key structure of ffmpeg - avframe
- 【DesignMode】组合模式(composite mode)
- 免费的聊天机器人API
- Spark SQL UDF function
- Room cannot create an SQLite connection to verify the queries
猜你喜欢
看抖音直播Beyond演唱会有感
MDK debug时设置数据实时更新
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
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
Free chat robot API
时间戳的拓展及应用实例
Classic CTF topic about FTP protocol
STM32 configuration after chip replacement and possible errors
Gd32f4xx UIP protocol stack migration record
Go learning --- structure to map[string]interface{}
随机推荐
Reading notes of the beauty of programming
Idea远程提交spark任务到yarn集群
Set data real-time update during MDK debug
Solve the problem of reading Chinese garbled code in sqlserver connection database
MySQL functions
MySQL存储引擎
小程序技术优势与产业互联网相结合的分析
uniapp开发,打包成H5部署到服务器
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
Spark AQE
7.5 simulation summary
Permission problem: source bash_ profile permission denied
Basic introduction and source code analysis of webrtc threads
[designmode] Decorator Pattern
Spark DF增加一列
数据分析思维分析方法和业务知识——分析方法(二)
Leetcode:20220213 week race (less bugs, top 10% 555)
synchronized 和 ReentrantLock
Ffmpeg learning - core module
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