【问题标题】:How to fetch the data which has multiple JSONObject in it如何获取其中包含多个 JSONObject 的数据
【发布时间】:2021-09-08 12:40:05
【问题描述】:

这是我必须获取的 JSON 响应:

{
  "markets": [
    {
      "exchange_id": "BINANCE",
      "symbol": "BTC-USDT",
      "price_unconverted": 37193.095,
      "price": 37228.28172528144,
      "change_24h": -4.547427178236877,
      "spread": 0.000026886707486166165,
      "volume_24h": 3422591459.036938,
      "status": "recent",
      "time": "2021-06-04T22:41:01"
    },
    {
      "exchange_id": "BINANCE",
      "symbol": "ETH-USDT",
      "price_unconverted": 2734.6549999999997,
      "price": 2737.2421349029846,
      "change_24h": -3.7432242168250704,
      "spread": 0.0003656761718006455,
      "volume_24h": 3244286756.710182,
      "status": "recent",
      "time": "2021-06-04T22:41:01"
    },
    ...
  ],
  "pagination": {
    "next": "10"
  }
}

我已经为第一个 JSON 对象编写了代码,其中包含数组列表。 我想知道如何在我的代码中获取第二个 JSON 对象,以便获取整个 JSON 对象值。

我的代码 ->

val queue = Volley.newRequestQueue(this) val url = "https://www.cryptingup.com/api/markets"

val jsonObjectRequest = object : JsonObjectRequest(Method.GET, url, null,
    Response.Listener {

        try {

        val markets = it.getJSONArray("markets")
        for (i in 0 until markets.length()) {

            val marketJsonObject = markets.getJSONObject(i)
            val marketObject = Market(
                marketJsonObject.getString("exchange_id"),
                marketJsonObject.getString("symbol"),
                marketJsonObject.getString("price_unconverted"),
                marketJsonObject.getString("price"),
                marketJsonObject.getString("change_24th"),
                marketJsonObject.getString("spread"),
                marketJsonObject.getString("volume_24th"),
                marketJsonObject.getString("status"),
                marketJsonObject.getString("time")

            )
           marketList.add(marketObject)
            
                recyclerAdapter =
                    MarketRecyclerAdapter(this, marketList)

                recyclerMarket.adapter = recyclerAdapter

                recyclerMarket.layoutManager = layoutManager

        }
        } catch (e: JSONException) {
            Toast.makeText(this, "$it JSON error Occured", Toast.LENGTH_LONG).show()
        }

    }, Response.ErrorListener {

        Toast.makeText(this, "Volley Error Occured $it", Toast.LENGTH_LONG).show()

    }){

    override fun getHeaders(): MutableMap<String, String> {
        val headers = HashMap<String, String>()
        headers["Content-type"] = "application/json"

        return headers
    }
}

queue.add(jsonObjectRequest)

}

【问题讨论】:

  • 将“recycler”代码移到for 循环之外,即在marketList 填满所有对象之前不要调用MarketRecyclerAdapter(...)

标签: java android json api kotlin


【解决方案1】:

试试下面的代码,它会对你有所帮助。

     try {
       
        val marketsArray: JSONArray = it.getJSONArray("markets")
        for (countItem in 0 until marketsArray.length()) {
            val returnObject = marketsArray.getJSONObject(countItem)
            val exchangeId =
                if (returnObject.isNull("exchange_id")) "" else returnObject.optString("exchange_id")
            val symbol =
                if (returnObject.isNull("symbol")) "" else returnObject.optString("symbol")
        }
        val pagination: JSONObject = it.getJSONObject("pagination")
        val next = pagination.getString("next")
    } catch (e: JSONException) {
        e.printStackTrace()
    }

【讨论】:

    【解决方案2】:

    另一方面,您是否尝试过使用 kotlinx 序列化将 JSON 对象(转换为字符串)映射到 POJO?

    https://kotlinlang.org/docs/serialization.html https://github.com/Kotlin/kotlinx.serialization

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多