我将Moshi用​​作Retrofit的转换器,但是对于一个特定的请求,它不起作用并且抛出了异常:

这是杰森

    "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
}

My model class

@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类使用构造函数,但出现错误。我也使用了list<Animaux>,但是没有用。

最佳答案

您的JSON包含Animaux列表,而不仅仅是一个列表。因此,您需要var animaux: List[Animaux],而不仅仅是Animaux



房间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

关于android - 预期为BEGIN_OBJECT,但在路径Moshi Json数据异常处为BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59078818/

10-11 03:50