鉴于我已经在使用Moshi将Retrofit响应从 JSON 字符串转换为对象,我是否还可以将Android 上的Room 数据库配置为将Moshi用于 TypeConverter 而不是手动键入 TypeConverter 函数并引用Moshi?
我一直在编写的一些我认为似乎多余的代码示例。
@TypeConverter
fun fromJsonToPictures(value: String): List<PictureObject>? {
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType(List::class.java, PictureObject::class.java)
val adapter = moshi.adapter<List<PictureObject>>(type)
return adapter.fromJson(value)
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 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"
}
]
Run Code Online (Sandbox Code Playgroud)
这就是我所做的:
实用程序
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 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Kotlin 构建 Android 应用程序,并使用 Retrofit 进行 API 调用,并且还使用 Moshi。但是在构建 Moshi 时,它显示 KotlinJsonAdapteryFactory - 未解析的引用,并且我无法使用 Moshi,因为它在这一行中给了我一个错误。知道为什么会发生这种情况吗?这些是我的 Moshi 依赖项:
implementation("com.squareup.moshi:moshi:1.12.0")
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0"
implementation "com.squareup.retrofit2:converter-moshi:2.5.0"
Run Code Online (Sandbox Code Playgroud)
这是构建 Moshi 和 Retrofit 的代码
private val moshi: Moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
val retrofit: Retrofit = Retrofit.Builder()
.client(getLoggingHttpClient())
.baseUrl(BASE_URL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
Run Code Online (Sandbox Code Playgroud) 有没有人知道如何让moshi生成带缩进的多行json(在config.json的上下文中供人类使用),所以从:
{"max_additional_random_time_between_checks":180,"min_time_between_checks":60}
Run Code Online (Sandbox Code Playgroud)
这样的事情:
{
"max_additional_random_time_between_checks":180,
"min_time_between_checks":60
}
Run Code Online (Sandbox Code Playgroud)
我知道其他json-writer实现可以这样做 - 但我想在这里坚持moshi以保持一致性
有一个json列表混合了普通的String和Image对象,如下所示:
{
"mixList": [
"string",
{
"imageUrl": "http://...",
"height": 320,
"width": 480
}
]
}
Run Code Online (Sandbox Code Playgroud)
如何用Moshi解析?
我希望有一个List<Data>,哪里StringData extends Data
和ImageData extends Data
在为 Android 项目配置 Kotlin 之后,我写了一个简单的MainActivity.kt. 它调用 Retrofit 来获取包含以下数据的 JSON 文件:
{
"message": "成功",
“用户”:{
“用户名”:“埃里克”
}
}
现在想用Moshi把JSON数据转换成Kotlin的类,所以这里有两个类来反映上面的JSON结构:
类用户(var 用户名:字符串) class UserJson(var message: String, var user: User)
以及 Moshi 的自定义类型适配器:
类用户适配器{
@FromJson fun fromJson(userJson: UserJson) : User {
Log.d("MyLog", "message = ${userJson.message}") // = 成功
Log.d("MyLog", "user = ${userJson.user}") // = null
返回 userJson.user
}
}
当它进入函数时fromJson(),userJson.message = "success"正如预期的那样。但奇怪的是,userJson.user就是null,这应该是User(username="Eric")。
我是 Moshi 和 Kotlin 的新手,我已经被这个问题困扰了大约 10 个小时。请帮帮我。谢谢你的帮助。
========================================
以下是 …
我正在尝试使用PolymorphicJsonAdapterFactoryJSON,它根据type字段提供不同的结构,如下所示。
{
"notifications": [
{
"type": "achievement",
"title": "New Achievement!",
"content": "You got new achievement Level 100! (100pt)"
"metadata": {
"category": "level",
"id": "level100",
"points": 100,
}
},
{
"type": "message",
"name": "message",
"content": "Cloud: hello, ~"
"metadata": {
"from": "Cloud",
"content": "hello, ~ "
}
},
{
"type": "new", <--- e.g. this is unknown type for app
"name": "something new",
"content": "You are informed something about. Please check app://~",
"deeplink": "app://~"
}
]
}
Run Code Online (Sandbox Code Playgroud)
假设我们的应用程序已经为一些已知的 …
我如何用moshi解析一个json结构,它具有在编译时未知的键:
"foo": {
"name": "hello",
"bar": {
"unknownKey1": {
"a": "1"
}
},
"unknownKey2": {
"b": "2"
},
"unknownKeyX": {
"c": "X"
}
},
"properties": {...}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用@FromJson适配器,JSONObject但日志只是说json是空的{}(我期望的地方{"unknownKey1": { ... etc ...})
class Foo {
@Json(name = "name")
String name;
@Json(name = "bar")
Bar bar;
static class Bar {
}
}
class BarAdapter {
@FromJson
Bar fromJson(JSONObject json) {
Log.d("xxx", "got " + json.toString());
return new Bar();
}
}
Run Code Online (Sandbox Code Playgroud)
一旦我可以进入json内部栏,我可以手动迭代它以添加到列表或其他东西(因为我不知道将有多少项).
像这样使用它:
Moshi …Run Code Online (Sandbox Code Playgroud) 默认情况下,ThreeTenABP.LocalDateTime将转换为
{"date":{"day":10,"month":4,"year":2018},"time":{"hour":3,"minute":34,"nano":115000000,"second":18}}
我可以写一个适配器来支持ISO日期字符串 2018-04-10T03:45:26.009
class LocalDateTimeAdapter {
@ToJson
fun toJson(value: LocalDateTime): String {
return FORMATTER.format(value)
}
@FromJson
fun fromJson(value: String): LocalDateTime {
return FORMATTER.parse(value, LocalDateTime.FROM)
}
companion object {
private val FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME
}
}
Run Code Online (Sandbox Code Playgroud)
如何编写可以支持这两种格式的适配器(fromJson)
{"date":{"day":10,"month":4,"year":2018},"time":{"hour":3,"minute":34,"nano":115000000,"second":18}}2018-04-10T03:45:26.009除了确定使用哪种格式之外fromJson,我很好奇Moshi如何在内部为LocalDateTime执行toJson/fromJson
如何在 Android kotlin、Moshi 和 Retrofit 中使用NESTED动态键解析嵌套 JSON ?
我从alpha-vantage得到这个 JSON 。
格式示例:
{
"Meta Data": {
"1. Information": "Intraday (15min) open, high, low, close prices and volume",
"2. Symbol": "AAME",
"3. Last Refreshed": "2019-11-18 16:00:00",
"4. Interval": "15min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (15min)": {//Dynamic - > Time Series (5min) / Time Series (30min)
"2019-11-18 16:00:00": {//Dynamic
"1. open": "1.6700",
"2. high": "1.6700",
"3. low": "1.5700",
"4. close": "1.5700",
"5. …Run Code Online (Sandbox Code Playgroud)