Gson从JSON转换为Java对象

时间:2015-05-01 15:08:54

标签: android json gson

(我对Gson和Json很新,所以请原谅我这是一个愚蠢的问题)

以下是我POST请求中的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

这很有效,所有值都放在我的response对象中。但显然这是非常繁琐的工作,特别是在我得到一个具有100个键/值对的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

但是,所有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 } 字段都为空...

有人可以帮忙吗?

这是我的FacebookUser类:

<sessionState mode="Off|InProc|StateServer|SQLServer"
              cookieless="true|false"
              timeout="number of minutes"
              stateConnectionString="tcpip=server:port"
              sqlConnectionString="sql connection string"
              stateNetworkTimeout="number of seconds"/>

2 个答案:

答案 0 :(得分:1)

您需要匹配确切的名称,您可以使用SerializedName注释

@SerializedName("user_id") String userId;

答案 1 :(得分: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,这应该有效