解析与SugarORM和GSON字符串ID字符串、SugarORM、GSON、ID

由网友(那些回不去的)分享简介:我用 GSON 以创建从JSON响应 SugarRecord 对象。我使用的是回报了一个名为id字段,但ID类型的API是一个字符串,不长(后端使用蒙戈)。下面是code我使用的是: GSON GSON =新GSON(); //或者使用新GsonBuilder()创建()。NutritionPlan目标= gson....

我用 GSON 以创建从JSON响应 SugarRecord 对象。我使用的是回报了一个名为id字段,但ID类型的API是一个字符串,不长(后端使用蒙戈)。

下面是code我使用的是:

  GSON GSON =新GSON(); //或者使用新GsonBuilder()创建()。NutritionPlan目标= gson.fromJson(jsonObject.getJSONObject(nutrition_day)的toString(),NutritionPlan.class); 

下面是我的JSON响应:

  {nutrition_day:{    ID:5342b4163865660012ab0000    START_ON:2014年4月8日,    protein_target:157,    sodium_limit:2000} 

有处理这种情况的好办法?我试过

  @Ignore长ID; 
Power query解析json字符串转化为表格和将excel表格生成json格式字符串

  @SerializedName(ID)串nutrition_plan_id; 

在我的模型,但没有帮助。任何熟悉糖ORM ,并知道如何处理与 ID 字段不长?

解决方案

在字符串中替换键ID是nutrition_day_id。您可以使用id JSON和ID SQL。

jsonObject.getJSONObject("nutrition_day").toString().replace(""id"",""nutrition_day_id"")

I'm using GSON to create a SugarRecord object from a json response. The API I'm using returns a field called "id", but the type of "id" is a string, not a long (the backend is using mongo).

Below is the code I'm using:

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

Below is my json response:

{
"nutrition_day": {
    "id": "5342b4163865660012ab0000",
    "start_on": "2014-04-08",
    "protein_target": 157,
    "sodium_limit": 2000
}

Is there a good way to handle this scenario? I tried

@Ignore 
long id;

and

@SerializedName("id")
String nutrition_plan_id;

in my model, but neither helped. Anyone familiar with Sugar ORM, and know how to deal with an id field that isn't a long?

解决方案

Replace the key "id" in the string to be "nutrition_day_id". You can use the id json and the id sql.

jsonObject.getJSONObject("nutrition_day").toString().replace(""id"",""nutrition_day_id"")

阅读全文

相关推荐

最新文章