将现有列表转换为 json/text 文件,以便继续读取该文件中的数据以生成列表
Convert existing List into a json/text file so moving forward read the data from that file to generate the List
如何将这个 List<Any>
变成 text/json 文件然后读取它?
由于此 FAQ 问题和答案将被硬编码,因此,我不希望将其直接设置在我的片段中。我打算将信息放入资产文件,然后从那里读取它以生成 List
我的List<Any>
val list: List<Any> = arrayListOf(
"General",
FAQ(faqQuestion = "ABC?", faqAnswer = "QWERTY", expanded = false),
FAQ(faqQuestion = "DEF?", faqAnswer = "ZXCVBN", expanded = false),
"Troubleshooting",
FAQ(faqQuestion = "ZXC?", faqAnswer = "POIUYT", expanded = false)
)
我不知道如何将其转换为 JSON
或文本文件并将其成功导出到此 List
。
目前我尝试过的是:
faq.json
[
"General",
{
"expanded":false,
"faqQuestion":"ABC?",
"faqAnswer":"QWERTY"
},
{
"expanded":false,
"faqQuestion":"DEF?",
"faqAnswer":"ZXCVBN"
}
]
文本文件faq.txt
"General",
FAQ(faqQuestion = "ABC?", faqAnswer = "QWERTY", expanded = false),
FAQ(faqQuestion = "DEF?", faqAnswer = "ZXCVBN", expanded = false),
"Troubleshooting",
FAQ(faqQuestion = "ZXC?", faqAnswer = "POIUYT", expanded = false)
从资产中获取的代码:文本
fun loadFromAsset(fileName: String, context: Context): Any? {
val any: Any
try {
val ins = context.getAssets().open(fileName)
val size = ins.available()
val buffer = ByteArray(size)
ins.read(buffer)
ins.close()
any = String(buffer, Charset.forName("UTF-8"))
} catch (ex: IOException) {
ex.printStackTrace()
return null
}
return any
}
var list: List<Any>?
loadFromAsset("faq.txt", requireContext())?.let {
list = arrayListOf(it)
}
JSON
fun loadFromAsset(fileName: String, context: Context): String? {
var json: String? = null
try {
val ins = context.getAssets().open(filename)
val size = ins.available()
val buffer = ByteArray(size)
ins.read(buffer)
ins.close()
json = String(buffer, Charset.forName("UTF-8"))
} catch (ex: IOException) {
ex.printStackTrace()
return null
}
return json
}
val list: List<Any> = Gson().fromJson(loadFromAsset("faq.json", requireContext()), Array<Any>::class.java).asList()
两种方式都不能按照原来的方式得到List<Any>
如果您的 json 仅包含 String
和 FAQ
元素
val list: List<Any> = gson.fromJson(json, JsonArray::class.java).map {
when (it) {
is JsonPrimitive -> it.asString as Any
else -> gson.fromJson(it, FAQ::class.java) as Any
}
}
如何将这个 List<Any>
变成 text/json 文件然后读取它?
由于此 FAQ 问题和答案将被硬编码,因此,我不希望将其直接设置在我的片段中。我打算将信息放入资产文件,然后从那里读取它以生成 List
我的List<Any>
val list: List<Any> = arrayListOf(
"General",
FAQ(faqQuestion = "ABC?", faqAnswer = "QWERTY", expanded = false),
FAQ(faqQuestion = "DEF?", faqAnswer = "ZXCVBN", expanded = false),
"Troubleshooting",
FAQ(faqQuestion = "ZXC?", faqAnswer = "POIUYT", expanded = false)
)
我不知道如何将其转换为 JSON
或文本文件并将其成功导出到此 List
。
目前我尝试过的是:
faq.json
[
"General",
{
"expanded":false,
"faqQuestion":"ABC?",
"faqAnswer":"QWERTY"
},
{
"expanded":false,
"faqQuestion":"DEF?",
"faqAnswer":"ZXCVBN"
}
]
文本文件faq.txt
"General",
FAQ(faqQuestion = "ABC?", faqAnswer = "QWERTY", expanded = false),
FAQ(faqQuestion = "DEF?", faqAnswer = "ZXCVBN", expanded = false),
"Troubleshooting",
FAQ(faqQuestion = "ZXC?", faqAnswer = "POIUYT", expanded = false)
从资产中获取的代码:文本
fun loadFromAsset(fileName: String, context: Context): Any? {
val any: Any
try {
val ins = context.getAssets().open(fileName)
val size = ins.available()
val buffer = ByteArray(size)
ins.read(buffer)
ins.close()
any = String(buffer, Charset.forName("UTF-8"))
} catch (ex: IOException) {
ex.printStackTrace()
return null
}
return any
}
var list: List<Any>?
loadFromAsset("faq.txt", requireContext())?.let {
list = arrayListOf(it)
}
JSON
fun loadFromAsset(fileName: String, context: Context): String? {
var json: String? = null
try {
val ins = context.getAssets().open(filename)
val size = ins.available()
val buffer = ByteArray(size)
ins.read(buffer)
ins.close()
json = String(buffer, Charset.forName("UTF-8"))
} catch (ex: IOException) {
ex.printStackTrace()
return null
}
return json
}
val list: List<Any> = Gson().fromJson(loadFromAsset("faq.json", requireContext()), Array<Any>::class.java).asList()
两种方式都不能按照原来的方式得到List<Any>
如果您的 json 仅包含 String
和 FAQ
元素
val list: List<Any> = gson.fromJson(json, JsonArray::class.java).map {
when (it) {
is JsonPrimitive -> it.asString as Any
else -> gson.fromJson(it, FAQ::class.java) as Any
}
}