Kotlin Json解析器
我在Kotlin中有这个JSON,我无法获取和解析。任何快速的帮助。求你了Kotlin Json解析器,json,kotlin,Json,Kotlin,我在Kotlin中有这个JSON,我无法获取和解析。任何快速的帮助。求你了 [{platform: {name: "mena-web",publishingRegion: "mena",platformGroup:"web",id: 2,countryCode: "AE",locales: {locale:["en_US","ar_AE"]}}}] 以下是我的数据类: data class Locales(var locale: ArrayList<String>) {} dat
[{platform: {name: "mena-web",publishingRegion: "mena",platformGroup:"web",id: 2,countryCode: "AE",locales: {locale:["en_US","ar_AE"]}}}]
以下是我的数据类:
data class Locales(var locale: ArrayList<String>) {}
data class Platform(var name: String, var publishingRegion: String, var platformGroup: String, var id: Int, var countryCode: String, var locales: Locales) {}
data class Json(var platform: Platform) {}
以下是我的JSON API接口:
interface Api {
@GET("/me.json2")
fun getGeo(callback: Callback<List<Json>>): Call<List<Json>>
}
这是我的建议:
class RestAPI(val api: Api) {
fun getNews(callback: Callback<List<Json>>) {
val call = api.getGeo(callback)
call.enqueue(callback)
}
}
这是我的电话:
try {
val api: RestAPI
val retrofit = Retrofit.Builder()
.baseUrl(PLATEFORM_URL)
.addConverterFactory(MoshiConverterFactory.create())
.build()
api = retrofit.create(RestApi::class.java)
val callback = object : Callback<List<Json>> {
override fun onResponse(call: Call<List<Json>>?, response: retrofit2.Response<List<Json>>?) {
response?.isSuccessful.let {
this@MainActivity.photos = response?.body()
}
}
override fun onFailure(call: Call<List<Json>>?, t: Throwable?) {
Log.e("MainActivity", "Problems calling API", t)
}
}
api.getGeo(callback)
// Log.e("Message", test.getNews().toList().toString())
} catch(e:Exception){
Log.e("Message", e.message)
}
谢谢大家!
我找到了答案,在更改解析器后,一切都正常工作
MoshiConverterFactory到GsonConverterFactory respones是将作为jsonResponse的字符串
try {
val rootArray = JSONArray(respones)
val mainObject=rootArray.getJSONObject(0)
val platformObject=mainObject.getJSONObject("platform")
val name=platformObject.getString("name")
val publishingRegion=platformObject.getString("publishingRegion")
val platformGroup=platformObject.getString("platformGroup")
val id=platformObject.getInt("id")
val countryCode=platformObject.getString("countryCode")
val localesObj=platformObject.getJSONObject("locales")
val localeArray=locales.getJSONArray("locale")
val stringOne=localeArray.getString(0)
val stringTwo=localeArray.getString(1)
} catch (e: JSONException){}
您得到的错误是什么?如果可能,请发布堆栈跟踪。您使用的是哪种解析器?@Miha_x64他使用的是Moshi,请参阅最后一段代码:.addConverterFactoryMoshiConverterFactory.createOk然后,回答第一个问题以获得帮助。请适当格式化代码部分,使其易于阅读。还要添加所有错误。