【发布时间】:2019-12-03 18:38:00
【问题描述】:
我正在尝试使用 moshi 将资产 Json 文件加载到我的项目中。但是,我不断收到以下错误:
com.squareup.moshi.JsonEncodingException:使用 JsonReader.setLenient(true) 在路径 $ 处接受格式错误的 JSON
我应该如何将以下 Json 加载到我的项目中?
json_file.json
[
{
"Name": "Show title",
"Description": "desc",
"Artwork": "link",
"URL": "feed url"
},
{
"Name": "Show title",
"Description": "desc",
"Artwork": "link",
"URL": "feed url"
}
]
这就是我所做的:
JsonUtil
object JsonUtil {
fun getAssetPodcasts(context: Context): List<JsonPodcast>? {
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val listType = Types.newParameterizedType(List::class.java, JsonPodcast::class.java)
val adapter: JsonAdapter<List<JsonPodcast>> = moshi.adapter(listType)
val file = "json_file.json"
val myjson = context.assets.open(file).bufferedReader().use{ it.readText()}
return adapter.fromJson(myjson)
}
@JsonClass(generateAdapter = true)
data class JsonPodcast(
val Name: String,
val Description: String,
val Artwork: String,
val URL: String
)
}
我的活动
getAssetPodcasts(this)
任何帮助将不胜感激!
【问题讨论】:
-
你不认为错误信息中有线索吗?
-
我知道,但是将我的代码与moshi guide 进行比较,我看不出我做错了什么。我不知道如何进行
标签: json kotlin android-assets moshi