gson 正在保存到共享首选项- java.lang.IllegalStateException:需要开始_OBJECT,但在路径$处为BEGIN_ARRAY

uhry853o  于 2022-11-06  发布在  Java
关注(0)|答案(2)|浏览(198)

我在这里有点疯狂...我有一系列片段,在这些片段中,我将用户输入存储到共享首选项中,以便在最后的onboarding片段中创建user/user dog。我认为我修复了模型以反映JSON响应,但我在尝试设置照片片段中的首选项时遇到错误,我不明白为什么。我认为这与照片模型有关。或者它是我的dog对象中的arrayList,但我不确定。
下面是我JSON响应对象:

{"dogID":"9","name":"dog","pedigree":"","breederName":"","color":"1","breed":"2","birthday":"1222","userID":"294","gender":"2","previousLitters":"0","isPregnant":"0","nextHeat":"0","previousSires":"1","litters":"0","isAKC":"1","user":{"userID":"294","email":"jonathanjosep@gmail.com","name":"John","termsAccepted":"1","userType":"0","messageNotifications":"0","matchesNotifications":"0","timeCreated":"2022-06-13 04:24:31","timeUpdated":"2022-06-22 14:01:15","zipCode":"10036","lat":"40.75921249","lng":"-73.98463440","distance":"64","profilePic":"http://res.cloudinary.com/matchbreed/image/upload/v1655094371/k1yapllywjburm5m0rhs.jpg","breeds":"Belgian Tervuren","isBreeder":"1","autoShare":"1","isSearching":"1","location":"49 St","dogs":[]},"dogHealth":{"firstTemperament":"0","secondTemperament":"0","thirdTemperament":"0","isVetEvaluated":"1","vaccinations":"0","hips":"0","elbows":"0","heart":"0","other":""},"dogTitles":["0"],"dogPhotos":[{"photoUrl":"ksldsldksdk","isPrimary":"1"},{"photoUrl":"url2","isPrimary":"0"}]}

这是我的狗模型:

@Parcelize
class DogModel(

    var birthday: String = "",
    var breed: String = "",
    var breederName: String = "",
    var color: String = "",
    var dogHealth: DogHealth = DogHealth(),
    var dogID: String = "",
    var dogPhotos: ArrayList<DogPhoto> = ArrayList(),
    var dogTitles: ArrayList<String> = ArrayList(),
    var gender: String = "",
    var isAKC: String = "",
    var isPregnant: String = "",
    var litters: String = "",
    var name: String = "",
    var nextHeat: String = "",
    var pedigree: String = "",
    var previousLitters: String = "",
    var previousSires: String = "",
    var user: User = User(),
    var userID: String = "",
):Parcelable {
    companion object {

    }

狗照片对象:

@Parcelize
data class DogPhoto(
    val isPrimary: String = "",
    var photoUrl: String = ""
): Parcelable{}

在设置变量-private var dog: DogModel = getDogPref()时,我在这里的片段中遇到错误
以下是我的偏好:

fun getDogPref(): DogModel {
    val u = getStringPref(PREF_DOG)
    if(u == ""){
        return DogModel()
    }
    val dog = JsonParser.parseString(u)
    return Gson().fromJson(dog, DogModel::class.java)
}

fun setDogPref(dog: DogModel) {
    val dogString = Gson().toJson(dog)//mapper.writeValueAsString(dog)
    setStringPref(PREF_DOG, dogString)
}

我不知道我在哪里...任何帮助都将不胜感激!!
这是我的DogHealth模型(如果需要的话):

@JsonIgnoreProperties(ignoreUnknown = true)
@Parcelize
data class DogHealth(
    @PrimaryKey(autoGenerate = true)
    var dogId: Int = 0,

    var elbows: String = "",
    var firstTemperament: String = "",
    var heart: String = "",
    var hips: String = "",
    var isVetEvaluated: String = "",
    var other: String = "",
    var secondTemperament: String = "",
    var thirdTemperament: String = "",
    var vaccinations: String = ""
): Parcelable {}
vcudknz3

vcudknz31#

我用下面的方法来做,对我来说效果很好
要保存

val dogJson = Gson().toJson(dog)
preferences.edit.putString("dog_${dog.id}", dogJson).apply()

阅读

val dogJson = preferences.getString("dog_$id")
dogJson ?: return
val dog = Gson().fromJson (dogJson, DogModel::class.java)

也许问题是你用Gson保存它,但用JsonParser阅读它。试着用Gson以同样的方式读取它。

z9smfwbn

z9smfwbn2#

Sharedpreferences用于键-值对中的原始数据,因此不能使用arrayList或对象作为值。
查看数据和文件存储概述。您可以选择存储结构化数据。https://developer.android.com/training/data-storage

相关问题