I use a JSON library called JSONObject
(I don\'t mind switching if I need to).
I know how to iterate over JSONArrays
, but when I parse JSO
I made my small method to log JsonObject fields, and get some stings. See if it can be usefull.
object JsonParser {
val TAG = "JsonParser"
/**
* parse json object
* @param objJson
* @return Map
* @throws JSONException
*/
@Throws(JSONException::class)
fun parseJson(objJson: Any?): Map {
val map = HashMap()
// If obj is a json array
if (objJson is JSONArray) {
for (i in 0 until objJson.length()) {
parseJson(objJson[i])
}
} else if (objJson is JSONObject) {
val it: Iterator<*> = objJson.keys()
while (it.hasNext()) {
val key = it.next().toString()
// If you get an array
when (val jobject = objJson[key]) {
is JSONArray -> {
Log.e(TAG, " JSONArray: $jobject")
parseJson(jobject)
}
is JSONObject -> {
Log.e(TAG, " JSONObject: $jobject")
parseJson(jobject)
}
else -> {
Log.e(TAG, " adding to map: $key $jobject")
map[key] = jobject.toString()
}
}
}
}
return map
}
}