Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
EventTracking
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杨林
EventTracking
Commits
b4f5dd56
Commit
b4f5dd56
authored
Jul 23, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解析json类
parent
d04927c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
src/main/scala/tools/json/JsonUtils.scala
src/main/scala/tools/json/JsonUtils.scala
+96
-0
No files found.
src/main/scala/tools/json/JsonUtils.scala
0 → 100644
View file @
b4f5dd56
package
tools.json
import
com.fasterxml.jackson.databind.ObjectMapper
import
com.fasterxml.jackson.module.scala.DefaultScalaModule
import
scala.collection.mutable
import
scala.util.
{
Failure
,
Success
,
Try
}
/**
* Created with IntelliJ IDEA.
* Class: Jackson
* Description: json解析类
* User: lin
* Date: 2021-07-19
* Time: 16:25
*/
object
JsonUtils
{
private
val
mapper
=
new
ObjectMapper
()
mapper
.
registerModule
(
DefaultScalaModule
)
/**
* 解析一级key
*
* @param str 要解析的字符串
* @return 解析结果
*/
def
parseFirstKey
(
str
:
String
)
:
mutable.HashMap
[
String
,
String
]
=
{
val
map
:
mutable.HashMap
[
String
,
String
]
=
mutable
.
HashMap
()
Try
{
// 获取所有节点
val
node
=
mapper
.
readTree
(
str
)
// 获取所有key
val
keys
=
node
.
fieldNames
()
// 循环所有节点
while
(
keys
.
hasNext
)
{
val
next
=
keys
.
next
()
// 解析一级key
map
+=
(
next
->
node
.
get
(
next
).
toString
)
}
}
match
{
case
Success
(
value
)
=>
map
case
Failure
(
exception
)
=>
map
}
}
/**
* 解析所有key
* 递归操作
*
* @param str 要解析的字符串
* @param res 解析结果map
* @return 返回res
*/
def
parseAllKey
(
str
:
String
,
res
:
mutable.HashMap
[
String
,
String
])
:
mutable.HashMap
[
String
,
String
]
=
{
Try
{
// 获取所有节点
val
node
=
mapper
.
readTree
(
str
)
// 获取节点所有key
val
keys
=
node
.
fieldNames
()
// 循环读取所有key
while
(
keys
.
hasNext
)
{
val
key
=
keys
.
next
()
val
chNode
=
node
.
get
(
key
)
// 如果子节点是json对象,递归解析子节点
if
(
chNode
.
isObject
)
{
parseAllKey
(
chNode
.
toString
,
res
)
}
else
{
res
+=
(
key
->
chNode
.
toString
)
}
}
}
match
{
// 解析成功
case
Success
(
value
)
=>
res
// 解析失败
case
Failure
(
exception
)
=>
res
}
}
/**
* 对象转json
*
* @param bean 要转的对象
* @return json结果
*/
def
parseBeanToString
(
bean
:
Any
)
:
String
=
mapper
.
writeValueAsString
(
bean
)
/**
* json转数组
*
* @param str json数组
* @return scala数组
*/
def
parseStringToArray
(
str
:
String
)
:
Array
[
Any
]
=
mapper
.
readValue
(
str
,
classOf
[
Array
[
Any
]])
}
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