【问题标题】:Kotlin Retrofit Moshi: returns nothingKotlin Retrofit Moshi:不返回任何内容
【发布时间】:2021-06-05 16:24:48
【问题描述】:

问题:

我需要解析下图,JSON,我使用Retrofit,Moshi,Kotlin,我使用数据类作为占位符,我将结果输出到LogCat,但是什么都没有输出。


JSON

{
      persons:[
               {
                "name": "Christian",
                "age": 5
               },
               {
                "name": "Marcus",
                "age": 10
               },
               {
                "name": "Norman",
                "age": 20
               }
             ]
}

数据类

data class Persons(
    @Json(name = "name") val name: String,
    @Json(name = "age")  val age: Int
)

API 服务

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

interface PersonApiService {
    @GET(ADDITIONAL_URL)
    fun getPersons(): Call<Persons>
}

object PersonApi {
    val retrofitService: PersonApiService by lazy {
        retrofit.create(PersonApiService::class.java)
    }
}

响应

override fun onResponse(call: Call<Persons>, response: Response<Persons>) {
         Log.i(TAG, "onResponse: ${response.body()}")                     // RETURNS NOTHING
}

【问题讨论】:

    标签: android json networking retrofit moshi


    【解决方案1】:

    解决方案:


    阅读this article,你就会明白一切。


    一点理论:

    记住并学会区分两种 Json:嵌套 JSON 对象 | JSON 对象数组

    Nested JSON Objecs:                      | Array of JSON Objects
                                             |          
           {                                 |          
             persons:[                       |          [  
                      {                      |           {
                        "name": "Christian", |             "name": "Christian",
                        "age": 5             |             "age": 5       
                      },                     |           },     
                      {                      |           {  
                        "name": "Marcus",    |             "name": "Marcus",
                        "age": 10            |             "age": 10
                      },                     |           }, 
                      {                      |           { 
                        "name": "Norman",    |             "name": "Norman",
                        "age": 20            |             "age": 20   
                      }                      |           }
                     ]                       |          ]
           }  
    

    如果您的 JSON 采用(JSON 对象数组)格式,您的实现将适合|


    嵌套 JSON 对象的数据类

    data class Persons(
        @Json(name = "persons") val persons: List<PersonProperty>
    )
    
    data class PersonProperty(
        @Json(name = "name") val name: String,
        @Json(name = "age")  val age: Int
    )
    

    响应

    override fun onResponse(call: Call<Persons>, response: Response<Persons>) {
             Log.i(TAG, "onResponse: ${response.body()}")                     // RETURNS List Persons
    }
    

    PS。 Vel_daN:喜欢什么你做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 2019-04-06
      • 2012-02-15
      • 2020-09-07
      • 2021-11-22
      • 2015-11-06
      • 2013-11-14
      • 2014-03-23
      相关资源
      最近更新 更多