GSON将json字符串转换为对象;得到零和0

时间:2011-12-30 06:55:44

标签: java android json gson

我在尝试反序列化JSON字符串时遇到问题。我正在使用gson

这是班级

public class Notebook {

  public static String DESCRIPTION = "description";

  @SerializedName("id")                 private long id;
  @SerializedName("user_id")            private long userId;
  @SerializedName("description")        private String description;
  @SerializedName("created_at")         private String railsCreated;
  @SerializedName("updated_at")         private String railsUpdated;    
  @SerializedName("created_android")    private String androidCreated;
  @SerializedName("updated_android")    private String androidUpdated;  
  private List<Note> notes;
  private boolean isUploaded = false;

  /*constructors, getters, and setters */

  public static void uploadNotebook(Notebook notebook) {
    MultipartEntity entity = new MultipartEntity();
    HttpResponse response;
    Notebook updatedNotebook = new Notebook();
    Gson gson = new Gson();
    try {
    StringBody description = new StringBody(notebook.getDescription());
    StringBody user = new StringBody("1");
    StringBody format = new StringBody("json");
//      StringBody androidCreated = new StringBody(notebook.getAndroidCreated().toString());
//      StringBody androidUpdated = new StringBody(notebook.getAndroidUpdated().toString());
    entity.addPart("description", description);
    entity.addPart("user_id", user);
    entity.addPart("format", format);
//          entity.addPart("android_created", androidCreated);
//          entity.addPart("android_updated", androidUpdated);
    response = WebService.postRequest(entity, WebService.POST_NOTEBOOK_URL);
    String json = EntityUtils.toString(response.getEntity());
    Log.i("Note Uploader", json);
    updatedNotebook = gson.fromJson(json, Notebook.class);
    if(updatedNotebook != null) {
        Log.i("Notebook ID", ""+updatedNotebook.getId());
        Log.i("Notebook userID", ""+updatedNotebook.getUserId());
        Log.i("Notebook Title", ""+updatedNotebook.getDescription());
        Log.i("Notebook CreatedAt", ""+updatedNotebook.getRailsCreated());
        Log.i("Notebook UpdatedAt", ""+updatedNotebook.getRailsUpdated());
        updatedNotebook.setUploaded(true);
    }
    } catch (UnsupportedEncodingException e) {
      Log.i("Note Uploader", e.getMessage());
    } catch (ClientProtocolException e) {
      Log.i("Note Uploader", e.getMessage());
    } catch (IOException e) {
      Log.i("Note Uploader", e.getMessage());
    }
  }
}

当我发布帖子时,Rails会回复以下json对象:

{
  "notebook":{
  "created_android":null,
  "created_at":"2011-12-30T06:35:42Z",
  "description":"Tatata",
  "id":12,
  "updated_android":null,
  "updated_at":"2011-12-30T06:35:42Z",
  "user_id":1
 }
}

但这是我尝试打印updatedNotebook字段时得到的结果。

12-30 14:35:42.673: INFO/Notebook ID(551): 0
12-30 14:35:42.673: INFO/Notebook userID(551): 0
12-30 14:35:42.673: INFO/Notebook Title(551): null
12-30 14:35:42.673: INFO/Notebook CreatedAt(551): null
12-30 14:35:42.673: INFO/Notebook UpdatedAt(551): null

我可能错过了一些东西......

3 个答案:

答案 0 :(得分:4)

您的课程与JSON不符。

您的JSON是一个包含一个字段(“notebook”)的对象,该字段的对象值包含您实际要查找的字段。

JSON需要看起来像:

{
  "created_android":null,
  "created_at":"2011-12-30T06:35:42Z",
  "description":"Tatata",
  "id":12,
  "updated_android":null,
  "updated_at":"2011-12-30T06:35:42Z",
  "user_id":1
}

正确映射到您的对象。 您还需要更正不匹配的字段名称。看起来你有那些。

修改:为了澄清,您现在拥有的JSON目前将映射到此:

public class NotebookContainer
{
    public Notebook notebook;
}

答案 1 :(得分:1)

我注意到你的几个序列化名称与json不匹配。 android_createdandroid_updated应首先更新以匹配json。这里可能存在其他问题,但这是一个问题。

答案 2 :(得分:1)

另一种方法:如果您可以控制Rails转储,则可以在开始通过ActiveRecord::Base.include_root_in_json = false生成json之前使用to_json忽略根元素