当前位置:网站首页>[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
边栏推荐
- STM32按键消抖——入门状态机思维
- notepad++正则表达式替换字符串
- Go learning - dependency injection
- An understanding of & array names
- Notepad + + regular expression replace String
- 2022.7.5-----leetcode. seven hundred and twenty-nine
- Go learning --- structure to map[string]interface{}
- JS can really prohibit constant modification this time!
- Classical concurrency problem: the dining problem of philosophers
- Ffmpeg learning - core module
猜你喜欢
Mysql - CRUD
Priority queue (heap)
[designmode] composite mode
How to solve the problems caused by the import process of ecology9.0
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Room cannot create an SQLite connection to verify the queries
Go learning - dependency injection
Key structure of ffmpeg - avframe
MySQL存储引擎
Permission problem: source bash_ profile permission denied
随机推荐
LeetCode 6006. Take out the least number of magic beans
Spark SQL null value, Nan judgment and processing
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Hudi of data Lake (2): Hudi compilation
Pointer - character pointer
Single source shortest path exercise (I)
2022-02-13 work record -- PHP parsing rich text
Leetcode:20220213 week race (less bugs, top 10% 555)
Spark DF adds a column
Room cannot create an SQLite connection to verify the queries
Yolov5、Pycharm、Anaconda环境安装
Gd32f4xx UIP protocol stack migration record
About the slmgr command
STM32 key chattering elimination - entry state machine thinking
如何利用Flutter框架开发运行小程序
Tools to improve work efficiency: the idea of SQL batch generation tools
What is information security? What is included? What is the difference with network security?
Set data real-time update during MDK debug
MySQL存储引擎
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统