Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
ShenghuoquanBusiness
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
ShenghuoquanBusiness
Commits
27a55f6f
Commit
27a55f6f
authored
Sep 06, 2021
by
shiyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化JsonUtil
parent
f6c328a9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
1 deletion
+30
-1
JsonUtils.kt
...ommon/src/main/java/com/yidian/bcommon/utils/JsonUtils.kt
+30
-1
No files found.
CommonLib/BCommon/src/main/java/com/yidian/bcommon/utils/JsonUtils.kt
View file @
27a55f6f
...
...
@@ -40,13 +40,42 @@ object JsonUtils {
* @param json
* @return
*/
fun
parseJSON2Map
(
json
:
JSONObject
):
Map
<
String
,
Any
?
>
{
fun
parseJSON2Map
Null
(
json
:
JSONObject
):
Map
<
String
,
Any
?
>
{
val
map
:
MutableMap
<
String
,
Any
>
=
HashMap
()
// 最外层解析
for
(
k
in
json
.
keys
())
{
when
(
val
v
=
json
[
k
])
{
is
JSONArray
->
{
// 如果内层还是json数组的话,继续解析
val
list
:
MutableList
<
Map
<
String
,
Any
?>>
=
ArrayList
()
for
(
i
in
0
until
v
.
length
())
{
val
jsonObject
=
v
.
getJSONObject
(
i
)
list
.
add
(
parseJSON2MapNull
(
jsonObject
))
}
map
[
k
.
toString
()]
=
list
}
is
JSONObject
->
{
// 如果内层是json对象的话,继续解析
map
[
k
.
toString
()]
=
parseJSON2Map
(
v
)
}
else
->
{
// 如果内层是普通对象的话,直接放入map中
map
[
k
.
toString
()]
=
v
}
}
}
return
map
}
/**
* 将json对象转换为HashMap
* @param json
* @return
*/
fun
parseJSON2Map
(
json
:
JSONObject
):
Map
<
String
,
Any
>
{
val
map
:
MutableMap
<
String
,
Any
>
=
HashMap
()
// 最外层解析
for
(
k
in
json
.
keys
())
{
when
(
val
v
=
json
[
k
])
{
is
JSONArray
->
{
// 如果内层还是json数组的话,继续解析
val
list
:
MutableList
<
Map
<
String
,
Any
>>
=
ArrayList
()
for
(
i
in
0
until
v
.
length
())
{
val
jsonObject
=
v
.
getJSONObject
(
i
)
list
.
add
(
parseJSON2Map
(
jsonObject
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment