在Kotlin中将JSONObject转换为JSONArray

时间:2018-09-24 20:29:53

标签: arrays json kotlin

Kotlin code

 private fun loadRecyclerViewData() {

    val request = Request.Builder().url(URL_Json).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback{

        override fun onResponse(call: Call?, response: Response?) {

            jsonData = response?.body()?.string()

            data class Info( val id: Int, val type: Int, val message: String, val typefield: Any, val hidden: Boolean,
                             val topSpacing: Int, val show: Any, val required: Boolean)
            //val gson = GsonBuilder().create()
            val gson = Gson()
            val persons = gson.fromJson(jsonData, Info::class.java)

            println("mensagem "+ jsonData) //return jsonObject 
            println("mensagem "+ persons)  //return null 

            runOnUiThread {
                //recyclerView?.adapter = FieldAdapter(feed)
            }
        }
        override fun onFailure(call: Call?, e: IOException?) {
            println("Fail request")
        }
    })

}

JSON链接: https://floating-mountain-50292.herokuapp.com/cells.json

大家好 我需要Kotlin的帮助。我想将JSONObjetct转换为JSONArray。但是我有return = null。 你能帮我吗?

第一个Println返回JSONObject正常,但是第二个println返回null。

println 1 https://imgur.com/Ww2bvAv

println 2 https://imgur.com/eViUe8q

0 个答案:

没有答案