提问人:Tyren Cleasby 提问时间:1/19/2023 最后编辑:Tyren Cleasby 更新时间:1/20/2023 访问量:394
将 object 的 String 变回对象 Kotlin
Turn String of object back into object Kotlin
问:
我有一个对象,我把它写到一个文件中,现在我想把我从文件中得到的那个字符串变成一个对象供我使用。
项目等级:
@Serializable
class DrinkItem {
@SerializedName("strAlcoholic")
val alcoholic: String? = null
@SerializedName("strIngredient1")
val ingredient1: String? = null
@SerializedName("strIngredient10")
val ingredient10: String? = null
@SerializedName("strIngredient11")
val ingredient11: String? = null
@SerializedName("strIngredient12")
val ingredient12: String? = null
@SerializedName("strIngredient13")
val ingredient13: String? = null
@SerializedName("strIngredient14")
val ingredient14: String? = null
@SerializedName("strIngredient15")
val ingredient15: String? = null
@SerializedName("strIngredient2")
val ingredient2: String? = null
@SerializedName("strIngredient3")
val ingredient3: String? = null
@SerializedName("strIngredient4")
val ingredient4: String? = null
@SerializedName("strIngredient5")
val ingredient5: String? = null
@SerializedName("strIngredient6")
val ingredient6: String? = null
@SerializedName("strIngredient7")
val ingredient7: String? = null
@SerializedName("strIngredient8")
val ingredient8: String? = null
@SerializedName("strIngredient9")
val ingredient9: String? = null
@SerializedName("strInstructions")
val instructions: String? = null
@SerializedName("strMeasure1")
val measurement1: String? = null
@SerializedName("strMeasure10")
val measurement10: String? = null
@SerializedName("strMeasure11")
val measurement11: String? = null
@SerializedName("strMeasure12")
val measurement12: String? = null
@SerializedName("strMeasure13")
val measurement13: String? = null
@SerializedName("strMeasure14")
val measurement14: String? = null
@SerializedName("strMeasure15")
val measurement15: String? = null
@SerializedName("strMeasure2")
val measurement2: String? = null
@SerializedName("strMeasure3")
val measurement3: String? = null
@SerializedName("strMeasure4")
val measurement4: String? = null
@SerializedName("strMeasure5")
val measurement5: String? = null
@SerializedName("strMeasure6")
val measurement6: String? = null
@SerializedName("strMeasure7")
val measurement7: String? = null
@SerializedName("strMeasure8")
val measurement8: String? = null
@SerializedName("strMeasure9")
val measurement9: String? = null
@SerializedName("strDrink")
val name: String? = null
@SerializedName("strDrinkThumb")
val thumbnail: String? = null
}
我使用这个函数将对象写入文件:
private fun writeToFile(fileName: String, byteArray: ByteArray){
val lineSeparator: String = System.getProperty("line.separator") as String
// File
val path = context!!.filesDir
val directory = File(path, "LET")
directory.mkdirs()
val file = File(directory, fileName)
//append drink to file
FileOutputStream(file, true).use {
it.write(byteArray)
it.write(lineSeparator.toByteArray())
}
}
在对对象完成函数后,它将转换为以下字符串:
{"alcoholic":"Alcoholic","ingredient1":"Apricot brandy","ingredient2":"Triple sec","ingredient3":"Lime","ingredient4":"Lime","instructions":"Shake all ingredients (except lime wedge) with ice and strain into a cocktail glass. Add the wedge of lime and serve.","measurement1":"1 oz ","measurement2":"1 oz ","measurement3":"Juice of 1 ","measurement4":"1 ","name":"After Dinner Cocktail","thumbnail":"https://www.thecocktaildb.com/images/media/drink/vtytxq1483387578.jpg"}
有没有一个函数或库可以帮助我将一个对象的字符串变成所述对象?
答:
1赞
Enowneb
1/20/2023
#1
您是否正在使用库将 JSON 字符串转换为输出 JSON 字符串?似乎 JSON 字符串中的键与您在 中命名的键不匹配。DrinkItem
@SerializedName()
您可以使用 Gson 库来处理 JSON 字符串和对象转换。
例如,如果您有相同的类,则可以将 your 转换为 JSON String,如下所示:DrinkItem
DrinkItem
// Define DrinkItem and set some of the attributes
val drinkItemTest = DrinkItem()
drinkItemTest.alcoholic = "Alcohol One"
drinkItemTest.ingredient1 = "Ingredient One"
// Use Gson library to convert Object to JSON String
val drinkItemTestString = Gson().toJson(drinkItemTest)
println(drinkItemTestString)
输出
{“strAlcoholic”:“酒精一”,“strIngredient1”:“成分一”}
要将 JSON 字符串转换回 ,您可以执行如下操作:DrinkItem
// Read the whole JSON String from your file here
val drinkItemString = "{\"strAlcoholic\":\"Alcohol One\",\"strIngredient1\":\"Ingredient One\"}"
// And make use of Gson library to convert your JSON String into DrinkItem Object
val drinkItem = Gson().fromJson(drinkItemString, DrinkItem::class.java)
评论
1赞
Tyren Cleasby
1/21/2023
Gson解决了我所有的问题,谢谢!
评论