简体   繁体   中英

GSON converting json string to object; getting nulls and 0s

I'm having trouble trying to deserialize a JSON string. I'm using gson

This is the class

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 replies with the following json object when I issue a post:

{
  "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
 }
}

But this is what I get when I try to print updatedNotebook fields.

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

I'm probably missing something...

Your class doesn't match the JSON.

Your JSON is an object that contains one field ("notebook") which has an object as its value that contains the fields you're actually looking for.

The JSON would need to look like:

{
  "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
}

To map correctly to your object. You also have mismatched field names that need to be corrected . Looks like you got those.

Edit: To clarify, the JSON you have now would currently map to this:

public class NotebookContainer
{
    public Notebook notebook;
}

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

I notice that several of your serialized names do not match the json. android_created and android_updated should have the updated first to match the json. There may be other problems here but that stands out as an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM