0
JSON響應嵌套屬性,我從服務器接收以這種形式確切的語法來得到與GSON
{"error":null,"id":1,"result":{"admin":false,"firstname":"Jason","id":346,"idHotel":109,"idVendor":null,"lastname":"Butcher","sessionkey":"3c8a17ae47a6d131b1a14b44a1d8f9a9","urlAvatar":"avatar_316_mjm.jpg","urlThumb":"thumb_316_mjm.jpg"}}
的響應,並希望得到的各種單打屬性,作爲原始嵌套JSON結果
例如
Boolean error=..;
String admin=....;
String idHotel=...;
我試圖做一個類以這種方式
public class HotelLogin {
public boolean error;
public int id;
public Result result;
//get and set
public static class Result {
public String lastname;
...
...//get and set
}
}
和我已經使用這個代碼捆綁反序列化的JSONObject serverResponse
HotelLogin loggedRs= new HotelLogin();
Gson gson = new Gson();
response = gson.fromJson(serverResponse, HotelLogin.class);
但在這一點上,我不知道怎麼弄內JSON的單一屬性。
如果我使用的代碼
Result user=login.getResult();
String lastname=user.getLastname();
得到一個空指針異常
問題是如何用Java做到這一點,而不是JavaScript – jsparks 2015-03-06 03:00:44