如何从json解析数据 [英] how to parse data from json

查看:121
本文介绍了如何从json解析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析json数据,但这有点奇怪,因为它没有显示正确的数据,但是如果我尝试在浏览器上调用json,它就具有正确的数据.

i tried to parse json data, but it kind of weird because it not show the right data but if i tried to called json on browser it has the right data.

所以这就是我解析json数据的方式

so this is how i parse the json data

doAsync {
        val url = localhost.getMovie()
        val request = okhttp3.Request.Builder().url(url).build()
        val client = OkHttpClient()

        uiThread {
            client.newCall(request).enqueue(object : Callback, okhttp3.Callback {
                override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
                    val body = response?.body()?.string()
                    println(body)
                    uiThread {
                        val gson = GsonBuilder().create()
                        val movieFeed = gson.fromJson(body, Movie2Response::class.java)

                        Log.v("body", ""+body)
                        Log.v("feed", ""+movieFeed.data)

                        uiThread {
                        }
                    }
                }

                override fun onFailure(call: okhttp3.Call?, e: IOException) {
                    println("failed")
                }

            })
        }
    }

电影响应

class Movie2Response (val data: MutableList<Movie2>)

电影

class Movie2 (
    @SerializedName("id")
    var movieId: String? = null,
    @SerializedName("description")
    var synopsis: String? = null,
    @SerializedName("release_date")
    var release: String? = null,
    @SerializedName("poster")
    var poster: String? = null,
    @SerializedName("genre")
    var genre: String? = null,
    @SerializedName("title")
    var title: String? = null

)

这就是我从json数据中得到的

and this is what i got from the json data

 V/body: {"data":[{"title":"Aquaman","description":""........
V/feed: [com.mqa.android.moviereview.model.Movie2@7509e04, com.mqa.android.moviereview.model.Movie2@890afed, com.mqa.android.moviereview.model.Movie2@9834e22, com.mqa.android.moviereview.model.Movie2@f02d0b3, com.mqa.android.moviereview.model.Movie2@d3b9670, com.mqa.android.moviereview.model.Movie2@4d55de9, com.mqa.android.moviereview.model.Movie2@cac2a6e, com.mqa.android.moviereview.model.Movie2@94fc50f, com.mqa.android.moviereview.model.Movie2@d9ba99c]

它直接显示在主体中,但在数组中显示为那样.请帮助它有什么问题.因为我想向旋转器显示标题数据

it shows right in body but in the array it show like that. please help what is wrong with it. because i want to show the title data to the spinner

推荐答案

您的代码运行良好,日志结果显示出来.真正的问题是日志功能Log.v("feed", ""+movieFeed.data).如果要显示漂亮的日志,则应通过以下方法覆盖Movie2类中的toString()方法:

Your code worked pretty well as the log results showed. The real problem is the log function Log.v("feed", ""+movieFeed.data). If you want to show pretty log, you should override the toString() method in Movie2 class by:

打开Movie2并右键单击editor-> Generate->,然后单击toString()覆盖它.

Open Movie2 and right click in the editor -> Generate -> then click toString() to override it.

对于Kotlin中的数据类,您只需在class关键字之前添加data.

For data class in Kotlin, you can just add data before class keyword.

这篇关于如何从json解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆