2012-02-15 119 views
3

我想解析一個JSON到一個對象。有兩個類:用戶和配置文件。用戶有一個配置文件的實例。Gson反序列化 - 試圖解析一個JSON到一個對象

所以現在有一個JSON來建立一個用戶對象。在此JSON內部列出了用戶和配置文件的屬性,您可以看到,配置文件和用戶都獲得了名爲List的HashMap。但是我想創建用戶和配置文件出此JSON的,但我得到這個異常:

//編輯:

我從配置文件和用戶刪除Map<String, String> links。所以現在我沒有得到任何錯誤,每個用戶都有一個檔案 - 但我仍然需要這些地圖。 GSON是否有可能區分json中的兩個列表資源,因爲它們具有相同的名稱?

//骯髒的黑客解決方案: ArrayList而不是HashMap沒有問題。不過,我決定 「手」 來解析JSON的這部分對象插入到我的HashMap ..

01-03 05:27:59.580: E/AndroidRuntime(4313): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 12 
01-03 05:27:59.580: E/AndroidRuntime(4313):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180) 

用戶:

public class User { 
    private String username; 
    private String slug; 
    private String email; 
    private Boolean emailVerified; 
    private Profile profile; 
    Map<String, String> links; 
    public User() 
    { 
     this.username = null; 
     this.slug = null; 
     this.email = null; 
     this.emailVerified = null; 
     this.profile = null; 
     this.links = new HashMap<String, String>(); 
    } 

    public String getUsername(){ 
     return this.username; 
    } 

    public String getSlug(){ 
     return this.slug; 
    } 

    public String getEmail(){ 
     return this.email; 
    } 

    public Boolean getEmailVerified(){ 
     return this.emailVerified; 
    } 

    public Profile getProfile(){ 
     return this.profile; 
    } 
} 

簡介:

public class Profile { 

    private Map<String, String> links; 
    private String name; 
    private String description; 
    private String gender; 
    private String status; 
    private String timezone; 
    private Bitmap icon; 

    public Profile() 
    { 
     this.name = null; 
     this.description = null; 
     this.gender = null; 
     this.status = null; 
     this.timezone = null; 
     this.links = new HashMap<String, String>(); 
    } 

    public String getName(){ 
     return this.name; 
    } 

    public String getDescription(){ 
     return this.description; 
    } 

    public String getGender(){ 
     return this.gender; 
    } 

    public String getStatus(){ 
     return this.status; 
    } 

    public String getTimezone(){ 
     return this.timezone; 
    } 
} 

一個例子JSON:

{ "email" : "[email protected]", 
    "emailVerified" : true, 
    "links" : [ { "href" : "http://xxx.de/api/users/4f3a73004bb67751bc000011", 
     "rel" : "self" 
     }, 
     { "href" : "http://xxx.de:/api/users/4f3a73004bb67751bc000011/followers", 
     "rel" : "https://xxx.com/rels/collection/follower" 
     }, 
     { "href" : "http://xxx.de/api/users/4f3a73004bb67751bc000011/friends", 
     "rel" : "https://xxx.com/rels/collection/friend" 
     }, 
     { "href" : "http://xxx.de/api/users/4f3a73004bb67751bc000011/activity_stream", 
     "rel" : "https://xxx.com/rels/activity_stream" 
     } 
    ], 
    "profile" : { "description" : "", 
     "gender" : "male", 
     "links" : [ { "href" : "xxx.de/uploads/profile_images/xxx.png", 
      "rel" : "https://xxx.com/rels/image" 
      }, 
      { "href" : "http://xxx.de/api/users/xxx/profile", 
      "rel" : "self" 
      } 
     ], 
     "name" : "Foo Bar", 
     "status" : "Status", 
     "timezone" : "CET" 
    }, 
    "slug" : "foobaar", 
    "username" : "foobaar" 
} 

訪問方法:

public static User parseUser(String json) { 
     JSONObject jsonObject; 
     Gson gson = new Gson(); 

     try { 
      jsonObject = new JSONObject(json); 
      Log.v(TAG,jsonObject.toString(2)); 
      User u = gson.fromJson(jsonObject.toString(), User.class); 
      return u; 

     } catch (JSONException e){ 
      Log.e(TAG, "There was an error parsing the JSON (USER)" + e); 
     } 
     return null; 
    } 

我的錯誤在哪裏?我可以用GSON像這樣使用HashMap嗎?在此先感謝

回答

2

使用Gson Deserializer類。他們很簡單:

爲了使這項工作,你必須確保解析器不會嘗試和序列化侵權對象 (在這種情況下你的地圖)。我會將你的地圖對象重命名爲_links或some,這樣序列化程序將跳過它。對於您的配置文件也要做同樣的事情。

一旦你這樣做,你必須反序列化,並確保包括在GSON對象解串器:

User u; 
    GsonBuilder gb = new GsonBuilder(); 
    gb.registerTypeAdapter(User.class, new UserDeserializer()); 
    Gson g = gb.create(); 
    u = g.fromJson(json, User.class); 


public class UserDeserializer implements JsonDeserializer<UserDeserializer> 
{ 
    @Override 
    public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) 
    { 
     User u = g.fromJson(json, User.class); 
     JsonObject jo = (JsonObject)json; 
     JsonElement je = jo.get("links"); 
     //iterate through the je element to fill your map. 
    } 

}