使用SugarORM和GSON解析字符串ID

cod*_*der 11 android json gson sugarorm

我正在使用json响应GSON创建一个SugarRecord对象.我正在使用的API返回一个名为"id"的字段,但"id"的类型是一个字符串,而不是long(后端使用mongo).

以下是我正在使用的代码:

Gson gson = new Gson(); // Or use new GsonBuilder().create();
NutritionPlan target = gson.fromJson(jsonObject.getJSONObject("nutrition_day").toString(), NutritionPlan.class);
Run Code Online (Sandbox Code Playgroud)

以下是我的json回复:

{
"nutrition_day": {
    "id": "5342b4163865660012ab0000",
    "start_on": "2014-04-08",
    "protein_target": 157,
    "sodium_limit": 2000
}
Run Code Online (Sandbox Code Playgroud)

有没有一种很好的方法来处理这种情况?我试过了

@Ignore 
long id;
Run Code Online (Sandbox Code Playgroud)

@SerializedName("id")
String nutrition_plan_id;
Run Code Online (Sandbox Code Playgroud)

在我的模型中,但没有帮助.任何熟悉Sugar ORM的人,知道如何处理id不长的领域?

eui*_*tam 0

我一直担心同样的问题,我找到的唯一解决方案是从我的 API 重命名 id 名称。从示例中尝试发送 Nutrition_plan_id 而不是 API 中的 id,它就可以完成任务。