当前位置:网站首页>[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
边栏推荐
- Search (DFS and BFS)
- Permission problem: source bash_ profile permission denied
- What is information security? What is included? What is the difference with network security?
- How spark gets columns in dataframe --column, $, column, apply
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- Ffmpeg captures RTSP images for image analysis
- FFmpeg抓取RTSP图像进行图像分析
- 常用API类及异常体系
- 如何解决ecology9.0执行导入流程流程产生的问题
- 看抖音直播Beyond演唱会有感
猜你喜欢
随机推荐
Pointer pointer array, array pointer
Notepad++ regular expression replacement string
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode 450 deleting nodes in a binary search tree
Ffmpeg captures RTSP images for image analysis
MDK debug时设置数据实时更新
MySQL之函数
Spark AQE
PHP determines whether an array contains the value of another array
Introduction of motor
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
Folding and sinking sand -- weekly record of ETF
Spark获取DataFrame中列的方式--col,$,column,apply
LeetCode 1598. Folder operation log collector
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
MySQL storage engine
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
MySQL存储引擎
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Go learning - dependency injection