Json与Scala类型的相互转换处理

目录
  1. 1. 测试实例

[TOC]

在开发过程中时常会有对json数据的一些处理,现做一些记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import net.minidev.json.parser.JSONParser
import scala.collection.JavaConversions.mapAsScalaMap
import scala.collection.mutable
import java.util

/**
* json utils
*/
object JsonUtils {


val mapper: ObjectMapper = new ObjectMapper()

def toJsonString(T: Object): String = {
mapper.registerModule(DefaultScalaModule)
mapper.writeValueAsString(T)
}

def getArrayFromJson(jsonStr: String) = {
JSON.parseArray(jsonStr)
}
def getObjectFromJson(jsonStr: String): JSONObject = {
JSON.parseObject(jsonStr)
}
/**
* 配合getObjectFromJson 使用把 JSONObject 变为 map
* @param jsonObj
* @return
*/
def jsonObj2Map(jsonObj:JSONObject): mutable.Map[String, String] = {
var map = mutable.Map[String, String]()
val itr: util.Iterator[String] = jsonObj.keySet().iterator()
while (itr.hasNext) {
val key = itr.next()
map += ((key, jsonObj.getString(key)))
}
map
}
/**
* json 字符串转成 Map
* #############有些情况下转换会有问题###############
* @param json
* @return
*/
def json2Map(json: String): mutable.HashMap[String,String] ={
val map : mutable.HashMap[String,String]= mutable.HashMap()
val jsonParser =new JSONParser()
//将string转化为jsonObject
val jsonObj: JSONObject = jsonParser.parse(json).asInstanceOf[JSONObject]

//获取所有键
val jsonKey = jsonObj.keySet()

val iter = jsonKey.iterator()

while (iter.hasNext){
val field = iter.next()
val value = jsonObj.get(field).toString

if(value.startsWith("{")&&value.endsWith("}")){
val value = mapAsScalaMap(jsonObj.get(field).asInstanceOf[util.HashMap[String, String]])
map.put(field,value.toString())
}else{
map.put(field,value)
}
}
map
}
/**
* map 转换成 json 字符串
* @param map
* @return
*/
def map2Json(map : mutable.Map[String,String]): String = {
import net.minidev.json.{JSONObject}
import scala.collection.JavaConversions.mutableMapAsJavaMap
val jsonString = JSONObject.toJSONString(map)
jsonString
}
}

测试实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def main(args: Array[String]) {

val json = "[{\"batchid\":305322456,\"amount\":20.0,\"count\":20},{\"batchid\":305322488,\"amount\":\"10.0\",\"count\":\"10\"}]"
val array: JSONArray = JsonUtils.getArrayFromJson(json)
println(array)
array.toArray().foreach(json=>{
println(json)
val jobj = json.asInstanceOf[JSONObject]
println(jobj.get("batchid"))
})

val jsonStr = "{\"batchid\":119,\"amount\":200.0,\"count\":200}"
val jsonObj: JSONObject = JsonUtils.getObjectFromJson(jsonStr)
println(jsonObj)

val jsonObj2: JSONObject = JsonUtils.getObjectFromJson("{'name':'Wang','age':18,'tag1':[{'tn1':'100','tn2':'101','ts':'ts01'},{'tn1':'100','tn2':'101','ts':'ts02'},{'tn1':'100','tn2':'101','ts':'ts03'}]}")
println(jsonObj2)
}
评论