当前位置:网站首页>Kotlin's JSON format parsing
Kotlin's JSON format parsing
2022-08-08 20:34:00 【Do not forget the original intention z】
kotlin parses json file
The first step: first import the KlaXon library, the official website address is: https://github.com/cbeust/klaxon
After the import is successful, we will try to compile a Json resource
Create a Gradle project import package
repositories {
jcenter()
}
dependencies {
implementation 'com.beust:klaxon:5.0.1'
}
After the import work is completed, it can be used
Step 2: Compilation start
val JsonObjec = json{
obj(“name” to “zhangsan” , “age” 33)} This is the first way to create something like { “name”: “zhangsan” “age”=30}
JsonObjec.put("sex",flast) This is the second way to create, type {"sex":flast}
val list = listOf(1,3)
JsonObject.put(a,list)The way of creating an array, similar to {"a":[1,3]} can achieve multiple nesting
Now the encoding has been completed, and the printout is a complete json format
If you want to parse Json files, it's actually very simple!You need to create a Parser to see the code specifically
//Original string created
val objectString = """{
“name” : “Joe”,
“age” : 23,
“flag” : true,
“array” : [1, 3],
"obj1" : { "a" : 1, "b" : 2 }
}"""
val parser = Parser.default
var stringParser = parser.parser(StringBuilder(objectString))The parsing has actually been completed
stringParser.string(“name”) prints out as Joe
stringParser.toJsonString prints as complete as a json file
But if we want to take out the array to 1, 3 to the value,Some operations are required
val JsonAry = stringParser.array< Int>(“array”) as JsonArray< Int> If you are sure to return this object, you can write as
The value to be taken out is JsonArt[0], and the value to be taken out is 1, and so on
But if we want to take out obj1 to the data
we need to execute
val jsonobj1 = stringPraser.obj(“obj1”) as JsonObject to get this, it is very simple to take out the number
obj1 corresponds to a and b is an int value, then we can use the int method to get it out
val int1 = jsonobj1.int("a") So we get the value 1, and the method for getting b is the same
In this way, we have finished encoding json and parsing json.
边栏推荐
猜你喜欢
随机推荐
内网渗透之代理转发
Kotlin注解
什么是仿射函数?
Bluu海鲜公司推出首批实验室培育的鱼类产品
自然堂品牌焕新升级,携手代言人王一博彰显美妆年轻新态度
MySQL权限管理
新库上线 | CnOpenDataA股上市公司基本信息数据
阿里财报中的饿了么:守正出奇
Kotlin annotations
DCT变换
Kotlin学习笔记
MySQL8 免安装版安装
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
学习笔记:第三章 栈与队列
PHP传递任意数量的函数参数
leveldb-impl:level0
方舟综合指令代码大全系统综合
有幸与美团大佬共同探讨单节点连接数超1.5W的问题
Ansible自动化运维工具(一)安装及模块
门外汉掌握数据分析处理技术的路线图