Commit 27a55f6f authored by shiyl's avatar shiyl

优化JsonUtil

parent f6c328a9
......@@ -40,13 +40,42 @@ object JsonUtils {
* @param json
* @return
*/
fun parseJSON2Map(json: JSONObject): Map<String, Any?> {
fun parseJSON2MapNull(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))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment