Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
goodsinfo
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
goodsinfo
Commits
363432d1
Commit
363432d1
authored
May 26, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加json解析方法
parent
b6e355cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
106 additions
and
0 deletions
+106
-0
src/main/scala/qm/tools/json/Jackson.scala
src/main/scala/qm/tools/json/Jackson.scala
+106
-0
No files found.
src/main/scala/qm/tools/json/Jackson.scala
View file @
363432d1
package
qm.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转换类
...
...
@@ -8,7 +14,107 @@ package qm.tools.json
* @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
)
}
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