当前位置:网站首页>[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
边栏推荐
- Gd32f4xx UIP protocol stack migration record
- Hudi of data Lake (2): Hudi compilation
- Problems and solutions of converting date into specified string in date class
- 如何解决ecology9.0执行导入流程流程产生的问题
- Room cannot create an SQLite connection to verify the queries
- AtCoder Beginner Contest 254【VP记录】
- Knowledge about the memory size occupied by the structure
- What is information security? What is included? What is the difference with network security?
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
猜你喜欢

notepad++正則錶達式替換字符串

Analysis of the combination of small program technology advantages and industrial Internet

Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?

OS i/o devices and device controllers

Gd32f4xx UIP protocol stack migration record

建立时间和保持时间的模型分析

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

MDK debug时设置数据实时更新

看抖音直播Beyond演唱会有感

Leetcode:20220213 week race (less bugs, top 10% 555)
随机推荐
Uniapp development, packaged as H5 and deployed to the server
Hudi of data Lake (2): Hudi compilation
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
Spark SQL空值Null,NaN判断和处理
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Search (DFS and BFS)
【线上小工具】开发过程中会用到的线上小工具合集
MySQL之函数
时间戳的拓展及应用实例
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
Spark SQL UDF function
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
XML Configuration File
LeetCode 1598. Folder operation log collector
Arduino六足机器人
AtCoder Beginner Contest 254【VP记录】
7.5 decorator
Knowledge about the memory size occupied by the structure
OpenCV经典100题