Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tb_goods
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
bigdata
tb_goods
Commits
b696c218
Commit
b696c218
authored
Jun 11, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解析json工具
parent
13a045da
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
0 deletions
+133
-0
src/main/scala/tools/json/Jackson.scala
src/main/scala/tools/json/Jackson.scala
+133
-0
No files found.
src/main/scala/tools/json/Jackson.scala
0 → 100644
View file @
b696c218
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
}
/**
* @ClassName: qm.tools.json
* @Description: json转换类
* @Author: LinYoung
* @Date: 2021/5/26
* @Time: 13:24
*/
object
Jackson
{
private
val
mapper
=
new
ObjectMapper
()
mapper
.
registerModule
(
DefaultScalaModule
)
/**
* 解析一级key
*
* @param str 要解析的字符串
* @return 解析结果
*/
def
parseFirstKey
(
str
:
String
)
:
mutable.Map
[
String
,
String
]
=
{
val
map
:
mutable.Map
[
String
,
String
]
=
mutable
.
Map
()
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.Map
[
String
,
String
])
:
mutable.Map
[
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
]])
}
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
val
str
=
"""
|{
| "name": "qmbigdata05",
| "cluster_name": "ES-Cluster",
| "cluster_uuid": "QdDDUfHFRdyse9OmjPsHxA",
| "version": {
| "number": "7.10.0",
| "build_flavor": "default",
| "build_type": "tar",
| "build_hash": "51e9d6f22758d0374a0f3f5c6e8f3a7997850f96",
| "build_date": "2020-11-09T21:30:33.964949Z",
| "build_snapshot": false,
| "lucene_version": "8.7.0",
| "minimum_wire_compatibility_version": "6.8.0",
| "minimum_index_compatibility_version": "6.0.0-beta1"
| },
| "tagline": "You Know, for Search",
| "list":[{
| "add":123456
| }]
|}
|"""
.
stripMargin
val
map
:
mutable.Map
[
String
,
String
]
=
mutable
.
Map
()
parseAllKey
(
str
,
map
)
println
(
parseFirstKey
(
str
))
println
(
map
)
}
}
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