预期为BEGIN_OBJECT,但为BEGIN_ARRAY

问题描述 投票:-1回答:1

我对JSON解析还很陌生。我正在尝试解析JSON。我遇到了一个问题,真的不知道如何解决。问题:预期是BEGIN_OBJECT,但在第1行第2列的路径处是BEGIN_ARRAY $。我觉得很简单,但我听不懂。我的代码:

class MainActivity : AppCompatActivity() {
    var request: Disposable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
            val o =
                Observable.create <String> {
                    val urlConnection =
                        URL("https://api.coinmarketcap.com/v1/ticker/?limit=30").openConnection() as HttpURLConnection
                    try {
                        urlConnection.connect()
                        if (urlConnection.responseCode != HttpURLConnection.HTTP_OK)
                            it.onError(RuntimeException(urlConnection.responseMessage))
                        else {
                            val str = urlConnection.inputStream.bufferedReader().readText()
                            it.onNext(str)
                        }
                    } finally {
                        urlConnection.disconnect()
                    }
                }
                    .map { Gson().fromJson(it, Feed::class.java) }
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())

            request = o.subscribe({
                for (item in it.items)
                    Log.d("test", "name: ${item.name}")

            }, {
                Log.d("test", "", it)
            })

    }
    override fun onDestroy() {
        super.onDestroy()
        request?.dispose()
    }
}
class Feed(
    var items: List<FeedItem>
)
class FeedItem(
    val id: String,
    val name: String,
    val symbol: String,
    val price_usd: String,
    val percent_change_1h: String,
    val percent_change_24h: String,
    val percent_change_7d: String
)

我希望接收的数组元素之一看起来像这样

{
    "id": "iota",
    "name": "IOTA",
    "symbol": "MIOTA",
    "price_usd": "1.96153",
    "24h_volume_usd": "64054500.0",
    "percent_change_1h": "-0.65",
    "percent_change_24h": "3.12",
    "percent_change_7d": "4.43",
    }

完整的错误文本如下:

Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
        at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
        at com.google.gson.Gson.fromJson(Gson.java:927) 
        at com.google.gson.Gson.fromJson(Gson.java:892) 
        at com.google.gson.Gson.fromJson(Gson.java:841) 
        at com.google.gson.Gson.fromJson(Gson.java:813) 
        at ru.skillbranch.cryptotrackertest.MainActivity$onCreate$o$2.apply(MainActivity.kt:39) 
        at ru.skillbranch.cryptotrackertest.MainActivity$onCreate$o$2.apply(MainActivity.kt:17) 
        at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:59) 
        at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onNext(ObservableCreate.java:67) 
        at ru.skillbranch.cryptotrackertest.MainActivity$onCreate$o$1.subscribe(MainActivity.kt:33) 
        at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40) 
        at io.reactivex.Observable.subscribe(Observable.java:10981) 
        at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33) 
        at io.reactivex.Observable.subscribe(Observable.java:10981) 
        at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96) 
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452) 
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) 
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
    ```
android json kotlin gson rx-java
1个回答
0
投票

您正在尝试将数组转换为单个值:

Gson().fromJson(it, Feed::class.java)

[您注意,您期望有一个数组,但是您要求它使用items字段解析对象。假设顶级JSON是一个数组,则应该使用Array<FeedItem>(或列表)作为要解析的类,而不是Feed

© www.soinside.com 2019 - 2024. All rights reserved.