我将Moshi用作翻新的转换器,但是对于一个特定的请求它不起作用,并且引发了异常: com.squareup.moshi.JsonDataException:预期为BEGIN_OBJECT,但在路径$处为BEGIN_ARRAY .animaux
这是杰森
"nb_animaux": 1,
"animaux": [
{
"code_iso": "768010103010004",
"photos": [],
"espece": {
"id": 1,
"fr": "Bovin",
"en": "Bovine"
},
"elevage": {
"parametre_geographique": {
"prefecture": "Oti",
"region": "Savane",
"commune": "Mango",
"pays": {
"fr": "TOGO",
"en": "TOGO"
},
"village": "Fonboro"
},
"type_elevage": {},
"code_elevage": "76801010301"
},
"sexe": "m",
"date_naissance": 1561932000,
"category": {
"fr": "Taureau",
"en": "Bull"
},
"proprietaire": {
"id": 10,
"fullname": "edem",
"adresse": "lome",
"telephone": "98665478"
}
}
],
"num_transport": "1/11/2019/TG",
"date_transport": 1574784132,
"traite": false
}
我的模型课
@Entity
data class Animal(
@field:PrimaryKey
var id: Long,
@ColumnInfo(name = "nb_animaux")
@Json(name = "nb_animaux")
var nbAnimaux : Int,
@Embedded
@get:Json(name = "animaux")
var animaux : Animaux,
@ColumnInfo(name = "num_transport")
@Json(name = "num_transport")
var numeroTransport : String,
@ColumnInfo(name = "date_transport")
@Json(name = "date_transport")
var dateTransport : Long,
var traite: Boolean
)
@JsonClass(generateAdapter = true)
data class Animaux(
@ColumnInfo(name = "animaux_id")
var id : Long,
@ColumnInfo(name = "code_iso")
@Json(name = "code_iso")
var codeIso : Long,
@Embedded
var espece : Espece,
@Embedded
var elevage : Elevage,
var sexe : String,
@ColumnInfo(name = "date_naisance")
@Json(name = "date_naisance")
var dateNaissance : Long,
@ColumnInfo(name = "race_pere")
@Json(name = "race_pere")
var racePere: String,
@ColumnInfo(name = "race_mere")
@Json(name = "race_mere")
var raceMere: String,
@Embedded
var category: Category,
@Embedded
var proprietaire: Proprietaire
)
/*{
constructor() : this(0, 0, Espece(0,"",""), Elevage(0, ParametreGeographique("","","",
Country("",""),""
),0), "", 0, "", "", Category(0,"",""),Proprietaire
(0,"","","",""))
}
*/
data class Espece(
@ColumnInfo(name = "id_espece")
@Json(name = "id_espece")
var id: Long,
var fr : String,
var en : String
)
data class Elevage(
@ColumnInfo(name = "id_elevage")
@Json(name = "id_elevage")
var id: Long,
@Embedded
var parametregeographique: ParametreGeographique,
@ColumnInfo(name = "code_elevage")
@Json(name = "code_elevage")
var codeElevage : Long
)
data class ParametreGeographique(
var prefecture : String,
var region : String,
var commune : String,
@Embedded
var pays : Country,
var village : String
)
data class Country(
@ColumnInfo(name = "fr_pays")
@Json(name = "fr_pays")
var fr : String,
@ColumnInfo(name = "en_pays")
@Json(name = "en_pays")
var en : String
)
data class Category (
@ColumnInfo(name = "id_category")
var id: Long,
@ColumnInfo(name = "fr_category")
var fr : String,
@ColumnInfo(name = "en_category")
var en : String
)
data class Proprietaire(
@ColumnInfo(name = "id_proprietaire")
var id: Long,
var fullname : String,
var telephone : String,
@ColumnInfo(name = "telephone_second")
var telephoneSecond : String,
@ColumnInfo(name = "photo_profile")
var photoProfil : String
)
改造电话
@GET("marche-betail/transport/{id}")
fun commandesDetailFromServer(@Path("id") id: Long?): Observable<Animal>
如何处理该错误。我尝试将构造函数用于类'Animaux',但出现错误。我也使用了列表,但是没有用。
答案 0 :(得分:0)
您的JSON有一个Animaux
列表,而不仅仅是一个列表。因此,您不仅需要var animaux: List[Animaux]
,还需要Animaux
。
如何在模型类中实现它。当我尝试出现此错误错误时,您能否显示如何执行此操作:
Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.util.List
房间doesn't support such fields directly。从技术上讲,您可以使它工作(请参阅Android room persistent library - how to insert class that has a List object field的答案),但是您的数据绝对足够复杂,以至于我不会尝试将单个类用于JSON和Room。最终会得到一个非常un-normalized database。