我在这里问是因为我认为这与 Moshi GitHub 问题无关。
我有卡片json变体 #1:
{ "id":"some id",
"type":"GENERIC_MESSAGE",
"data": { "title":"Small error",
"message":"Please update some info",
"type":"ERROR"
}
}
和变体 #2:
{ "id":"some id",
"type":"DATA_SCIENCE",
"data": { "cardData":
{ "message":"You spent...",
"title":"...last month"}
}
}
}
这是我的通用 JSON 适配器代码:
public class CardJsonAdapter
{
@Keep
static class CardJson
{
String id;
Card.Type type;
JSONObject data; <--- HERE is my problem
}
@FromJson
Card fromJson(@NonNull final CardJson json)
throws IOException
{
try
{
CardData data = getCardData(json);
return new Card(json.id, json.type, data);
}
catch (JSONException e)
{
throw new JsonDataException("Can not parse Card json");
}
}
private CardData getCardData(final @NonNull CardJson json)
throws JSONException
{
CardData data = null;
switch (json.type)
...
}
...
}
所以通过卡片type
,我已经知道如何解析data
对象。但我不知道如何在数据中获得通用的东西。我无法将类型设置为,String
因为Moshi崩溃了BEGIN_OBJECT not expected error
,我不能说它也因第二个jsonMap<String, Object>
的错误而失败。并且不会因解析而崩溃,但解析后完全为空。JsonObject
我还找不到任何东西,所以我在征求你的意见