2017-12-07 2 views
1

나는이 같은 JSON 문자열을 구문 분석을 시도하고 있었다 :연료, 코 틀린, GSON, BEGIN_ARRAY 예상하지만 BEGIN_OBJECT 라인에서 1

{ 
    "count": 1, 
    "items": [ 
     { 
      "organization_id": 6972979, 
      "organization_name": "Lorem ipsum dolor sit amet, consectetur adipisicing elit", 
     } 
    ] 
} 

그리고 코 틀린 클래스 :

class LoremModel { 
    var count: Int? = null 
    var items: List<Lorem>? = null 

    class Lorem { 
     var organization_id: Int? = null 
     var organization_name: String? = null 

     constructor(organization_id: Int?, organization_name: String?) { 
      this.organization_id = organization_id 
      this.organization_name = organization_name 
     } 
    } 

    class ListDeserializer : ResponseDeserializable<List<LoremModel>> { 
     override fun deserialize(content: String) = Gson().fromJson<List<LoremModel>>(content, object : TypeToken<List<LoremModel>>() {}.type) 
    } 
} 

연료를 부분 :

Fuel.get("/lorem/search", listOf("keywords" to keyword, "category" to category, "pageNum" to "1", "pageSize" to "10")). 
     responseObject(LoremModel.ListDeserializer()) { request, _, item -> 
     } 

하지만 오류가 점점 오전 :

0123을

어떻게 해결할 수 있습니까?

답변

1

귀하의 JSON

{ 
    "count": 1, 
    "items": [ 
     { 
      "organization_id": 6972979, 
      "organization_name": "Lorem ipsum dolor sit amet, consectetur adipisicing elit", 
     } 
    ] 
} 

는 JSON 개체가 아닌 JSON 배열을 나타냅니다.

그래서 대신 LoremModelList의 유형으로 직렬화하려고은

Gson().fromJson<List<LoremModel>>(content, object : TypeToken<List<LoremModel>>() {}.type) 

당신은 유형 LoremModel의 객체로 역 직렬화한다 객체. 이렇게 할 수 있습니다 :

Gson().fromJson(content, LoremModel::class.java)