вы дали неправильный JSON одно закрытие "}" Я думаю, что вы скопировали неправильно
создать класс Gson2Application
class Gson2Application
val jj = "{\n" +
"\"status\": 0,\n" +
"\"error_message\": null,\n" +
"\"data\": {\n" +
"\"1\": {\n" +
" \"7\": [\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"person_id\": \"3\",\n" +
" \"friend_id\": \"2\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"}"
data class Model(val id: Long, val person_id: Long, val friend_id: Long)
fun main(args: Array<String>) {
var myJson = JsonParser().parse(jj).asJsonObject
val data = myJson.getAsJsonObject("data").get("1")
val type = object : TypeToken<Map<String, ArrayList<Model>>>() {}.type // the Map is interfacem then gson get default LinkedTreeMap extedn AbstractMap<K,V> implements Map
val fromMapDefault = Gson().fromJson<Map<String, List<Model>>>(data, type)
printResult(fromMapDefault)
// result
// {{7=[Model(id=1, person_id=3, friend_id=2)]} class com.google.gson.internal.LinkedTreeMap--> 1}
// {[Model(id=1, person_id=3, friend_id=2)] class java.util.ArrayList--> 1}
// {Model(id=1, person_id=3, friend_id=2) class pl.jac.gson2.Model --> 2}
// {1 long--> 3}
val typeHashMap = object : TypeToken<HashMap<String, ArrayList<Model>>>() {}.type // the HashMap is an implementation
val fromHashMap = Gson().fromJson<Map<String, List<Model>>>(data, typeHashMap)
printResult(fromHashMap)
//result
// {{7=[Model(id=1, person_id=3, friend_id=2)]} class java.util.HashMap--> 1}
// {[Model(id=1, person_id=3, friend_id=2)] class java.util.ArrayList--> 1}
// {Model(id=1, person_id=3, friend_id=2) class pl.jac.gson2.Model --> 2}
// {1 long--> 3}
}
private fun printResult(fromMapDefault: Map<String, List<Model>>) {
println("""
result
{${fromMapDefault} ${fromMapDefault?.javaClass}--> 1}
{${fromMapDefault["7"]} ${fromMapDefault["7"]?.javaClass}--> 1}
{${fromMapDefault["7"]?.get(0)} ${fromMapDefault["7"]?.get(0)?.javaClass} --> 2}
{${fromMapDefault["7"]?.get(0)?.id} ${fromMapDefault["7"]?.get(0)?.id?.javaClass}--> 3}
""".trimIndent())
}
}
private fun printResult(fromMapDefault: Map<String, List<Model>>) {
println("""
result
{${fromMapDefault} ${fromMapDefault?.javaClass}--> 1}
{${fromMapDefault["7"]} ${fromMapDefault["7"]?.javaClass}--> 1}
{${fromMapDefault["7"]?.get(0)} ${fromMapDefault["7"]?.get(0)?.javaClass} --> 2}
{${fromMapDefault["7"]?.get(0)?.id} ${fromMapDefault["7"]?.get(0)?.id?.javaClass}--> 3}
""".trimIndent())
}
я перехожу на вашу модель ClassStatus
data class ClassStatus(val status: Int, val error_message: String?, val data: Map<String,Map<String, ArrayList<Model>>>)
data class Model(val id: Long, val person_id: Long, val friend_id: Long)
fun main(args: Array<String>) {
val fromJson = Gson().fromJson<ClassStatus>(jj, ClassStatus::class.java)
printResult(fromJson)
//result
//ClassStatus(status=0, error_message=null, data={1={7=[Model(id=1, person_id=3, friend_id=2)]}}) class pl.jac.gson2.not.ClassStatus--> 1
//{1={7=[Model(id=1, person_id=3, friend_id=2)]}} class com.google.gson.internal.LinkedTreeMap--> 2
//{7=[Model(id=1, person_id=3, friend_id=2)]} class com.google.gson.internal.LinkedTreeMap--> 3
//[Model(id=1, person_id=3, friend_id=2)] class java.util.ArrayList--> 4
//Model(id=1, person_id=3, friend_id=2) class pl.jac.gson2.not.Model--> 5
//1 long--> 6
}
private fun printResult(fromMapDefault: ClassStatus) {
println("""
result
${fromMapDefault} ${fromMapDefault.javaClass}--> 1
${fromMapDefault.data} ${fromMapDefault.data.javaClass}--> 2
${fromMapDefault.data["1"]} ${fromMapDefault.data["1"]?.javaClass}--> 3
${fromMapDefault.data["1"]!!["7"]} ${fromMapDefault.data["1"]!!["7"]!!.javaClass}--> 4
${fromMapDefault.data["1"]?.get("7")!![0]} ${fromMapDefault.data["1"]!!["7"]!![0].javaClass}--> 5
${fromMapDefault.data["1"]?.get("7")!![0].id} ${fromMapDefault.data["1"]!!["7"]!![0].id!!.javaClass}--> 6
""".trimIndent())
}
val jj = "{\n" +
"\"status\": 0,\n" +
"\"error_message\": null,\n" +
"\"data\": {\n" +
"\"1\": {\n" +
" \"7\": [\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"person_id\": \"3\",\n" +
" \"friend_id\": \"2\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"}"