我如何使用GSON解析这个json?
[
[
456239452,180279471,"fsdgdfgdfg","T",
"M",244,0,0,"",0,82,"","[]","fgfdgdfg",10
],
[
456239451,180279471,"9wnntiu5vu9mBeDiBej4swfcztjOS4nJe",
"R","s",253,0,0,"",0,82,"","[]","cf46","g",14
]
]
我的代码:
data class Data(val list: List<List<String>>)
Gson().fromJson(json, Data::class.java).let {
it.list.forEach {
it.forEach {
println(it)
}
}
}
我的堆栈跟踪
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第2行路径$ BEGIN_ARRAY $ 在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:224) 在com.google.gson.Gson.fromJson(Gson.java:888) 在com.google.gson.Gson.fromJson(Gson.java:853) 在com.google.gson.Gson.fromJson(Gson.java:802) 在com.google.gson.Gson.fromJson(Gson.java:774) 在dchistov.vkmusic.music.MusicFragment $ initAudio $ 1.doResume(MusicFragment.kt:58) at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54) at kotlinx.coroutines.experimental.DispatchedTask $ DefaultImpls.run(Dispatched.kt:161) 在kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:25) at java.util.concurrent.ForkJoinTask $ RunnableExecuteAction.exec(ForkJoinTask.java:1412) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:285) 在java.util.concurrent.ForkJoinPool $ WorkQueue.runTask(ForkJoinPool.java:1152) 在java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1990) 在java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1938) 在java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) 引起:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第2行路径$ BEGIN_ARRAY 在com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385) 在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:213) 在com.google.gson.Gson.fromJson(Gson.java:888) 在com.google.gson.Gson.fromJson(Gson.java:853) 在com.google.gson.Gson.fromJson(Gson.java:802) 在com.google.gson.Gson.fromJson(Gson.java:774) 在dchistov.vkmusic.music.MusicFragment $ initAudio $ 1.doResume(MusicFragment.kt:58) at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54) at kotlinx.coroutines.experimental.DispatchedTask $ DefaultImpls.run(Dispatched.kt:161) 在kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:25) at java.util.concurrent.ForkJoinTask $ RunnableExecuteAction.exec(ForkJoinTask.java:1412) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:285) 在java.util.concurrent.ForkJoinPool $ WorkQueue.runTask(ForkJoinPool.java:1152) 在java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1990) 在java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1938) 在java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
答案 0 :(得分:1)
看起来你唯一能做的就是把它解析为List<List<Any>>
,然后按索引访问元素。
要实现这一目标,您应该执行类似
的操作import java.lang.reflect.Type
import com.google.gson.reflect.TypeToken
Type listType = object: TypeToken<List<List<Any>>>(){}.type
List<List<Any>> list = Gson().fromJson(jsonArray, listType)