[英]Converting to JSONObject and back in Kotlin without Library
I'm trying to convert a nested Map to a JSONObject like so:我正在尝试将嵌套的 Map 转换为 JSONObject,如下所示:
fun convertToJson(input: Map<String, Any>): JSONObject {
val jsonObject = JSONObject()
input.forEach { (key, value) ->
if (value is Map<*, *>) {
val iterator = value.entries.iterator()
while (iterator.hasNext()) {
val pairs = iterator.next()
(pairs.key as? String)?.let { k ->
pairs.value?.let { v ->
jsonObject.put(k, v)
}
}
}
}
jsonObject.put(key, value)
}
return jsonObject
}
(I tried following this example Putting HashMap<String, object> in jsonobject ) (我尝试按照这个示例将 HashMap<String, object> 放入 jsonobject )
I call it like so我这样称呼它
val input = mapOf(
"key1" to mapOf("inner_key1" to "foo"))
val output = convertToJson(input)
What I don't understand, is why is我不明白的是为什么
output.optJSONObject("key1")
null? output.optJSONObject("key1")
null? From what I understand, output.opt("key1")
returns a Map<*, *>
.据我了解,
output.opt("key1")
返回Map<*, *>
。
That's about as far as I got.这就是我所得到的。 I'm not sure if my
convertToJson
needs fixing, or if my understanding needs correcting, in that, optJSONObject
should not be used for nested types and I should use opt
if I know the type will be a Map
.我不确定我的
convertToJson
是否需要修复,或者我的理解是否需要更正,因为optJSONObject
不应该用于嵌套类型,如果我知道类型将是Map
,我应该使用opt
。
Try to put JSONObject
inside JSONObject
instead of Map<*, *>
尝试将
JSONObject
放入JSONObject
而不是Map<*, *>
fun convertToJson(input: Map<String, Any>): JSONObject {
val jsonObject = JSONObject()
input.forEach { (key, value) ->
if (value is Map<*, *>) {
val iterator = value.entries.iterator()
val childObject = JSONObject()
while (iterator.hasNext()) {
val pairs = iterator.next()
(pairs.key as? String)?.let { k ->
pairs.value?.let { v ->
childObject.put(k, v)
}
}
}
jsonObject.put(key, childObject)
} else {
jsonObject.put(key, value)
}
}
return jsonObject
}
The logic in your code doesn't put keys of a Map
into a nested object, but directly into jsonObject
.您的代码中的逻辑不会将
Map
的键放入嵌套的 object 中,而是直接放入jsonObject
中。 And then it also calls jsonObject.put(key, value)
even for a Map
(which I am a bit surprised doesn't throw an exception, because it isn't one of the allowed types :然后它甚至为
Map
也调用jsonObject.put(key, value)
(我有点惊讶它没有抛出异常,因为它不是允许的类型之一:
Object: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null.
Object: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null. May not be Double#isNaN() or Double#isInfinite().
可能不是 Double#isNaN() 或 Double#isInfinite()。
). )。
Assuming keys of any nested Map
are strings, I would just do it recursively:假设任何嵌套的
Map
的键都是字符串,我会递归地做:
@Suppress("UNCHECKED_CAST")
fun convertToJson(input: Map<String, Any>): JSONObject {
val jsonObject = JSONObject()
input.forEach { (key, value) ->
value1 = if (value is Map<*, *>)
convertToJson(value as Map<String, Any>)
else
value
jsonObject.put(key, value1)
}
return jsonObject
}
(this really should be extended to handle List
s, null
, etc. in addition, but that's not part of the question) (这确实应该扩展到处理
List
s、 null
等,但这不是问题的一部分)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.