2015-05-01 191 views
0

(我很新的GSON和JSON,所以請原諒我,如果這是一個愚蠢的問題)GSON轉換從JSON到Java對象

這裏是我的POST請求response

String getResponse = ({"user_id":"1","device_id":"0","user_name":"jdoe","first_name":"John","last_name":"Doe"}); 

得到response後,我用下面的方法把JSON值進入我Java對象:

FacebookUser facebookUser = new FacebookUser(); 

JSONObject responseObject = new JSONObject(getResponse); 

    Iterator<String> iter = responseObject.keys(); 
    while (iter.hasNext()) { 
     String key = iter.next(); 

     switch (key) { 

      case "user_id": 
       try { 
        Object value = responseObject.get(key); 
        facebookUser.setUserId((String) value); 
       } catch (JSONException e) { 
        Log.d("JsonException", "error" + e.toString()); 
       } 
       break; 

      case "device_id": 
       try { 
        Object value = responseObject.get(key); 
        facebookUser.setDeviceId((String) value); 
       } catch (JSONException e) { 
        Log.d("JsonException", "error" + e.toString()); 
       } 
       break; 

      case "user_name": 
       try { 
        Object value = responseObject.get(key); 
        facebookUser.setUsername((String) value); 
       } catch (JSONException e) { 
        Log.d("JsonException", "error" + e.toString()); 
       } 
       break; 

      case "first_name": 
       try { 
        Object value = responseObject.get(key); 
        facebookUser.setFirstName((String) value); 
       } catch (JSONException e) { 
        Log.d("JsonException", "error" + e.toString()); 
       } 
       break; 

      case "last_name": 
       try { 
        Object value = responseObject.get(key); 
        facebookUser.setLastName((String) value); 
       } catch (JSONException e) { 
        Log.d("JsonException", "error" + e.toString()); 
       } 
       break; 
     } 
    }  

這很好,所有的值都放在我的FacebookUser對象中。但顯然這是非常繁瑣的工作,尤其是在我將得到帶有100個鍵/值對的response的情況下。

所以我試着用Gson,使這項工作一下子:

 String getResponse = ({"user_id":"1","device_id":"0","user_name":"jdoe","first_name":"John","last_name":"Doe"}); 

     FacebookUser facebookUser = new FacebookUser(); 

     Gson gson = new GsonBuilder() 
       .disableHtmlEscaping() 
       .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) 
       .setPrettyPrinting() 
       .serializeNulls() 
       .create(); 

     FacebookUser facebookUser = gson.fromJson(getResponse, 
         FacebookUser.class); 

然而,所有的facebookUser字段爲空...

是否有人可以幫忙嗎?

這裏是我FacebookUser類:

public class FacebookUser implements Serializable{ 

    String userId; 
    String deviceId; 
    String username; 
    String firstName; 
    String lastName; 

    public FacebookUser() { //default ctor 
    } 

    ... a bunch of getters 
    ... a bunch of setters 
} 
+1

您能不能告訴你FacebookUser類? –

+0

我添加了FacebookUser類 – user2456977

回答

1

你需要精確匹配的名稱,你可以使用SerializedName註釋

@SerializedName("user_id") String userId; 
+0

啊,所以我在我的FacebookUser類中執行此操作...僅僅是我有一個默認的ctor還是我需要一個需要初始化所有字段的ctor? – user2456977

+0

你不需要聲明任何其他的ctor,gson在後面使用反射,你的ctor可以是任何東西。 –

0

GSON不知道找到匹配,因爲下劃線的JSON字段屬性,你必須解釋如何使用_

`

final GsonBuilder builder = new GsonBuilder(); 

builder.setDateFormat(DateFormat.LONG); 
builder.setPrettyPrinting(); 
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) 
final Gson gson = builder.create(); 

FacebookUser facebookUser = gson.fromJson(getResponse,FacebookUser.class); 

`

有了這個GSON這應該作品