Android 访问另一个JSON对象中的JSON对象
我的Android应用程序中有这样一个JSON:Android 访问另一个JSON对象中的JSON对象,android,json,Android,Json,我的Android应用程序中有这样一个JSON: { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJtZWhkaS5hZ291emFsQGdtYWlsLmNvbSIsImV4cCI6MTQ2ODU1NTU4MjQ5MX0.A6-xNIZdbPv9mpjLO9jnnfbAeq1y-DC08SBUt2xBnYo", "expires": 1468555582491, "user": { "i
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJtZWhkaS5hZ291emFsQGdtYWlsLmNvbSIsImV4cCI6MTQ2ODU1NTU4MjQ5MX0.A6-xNIZdbPv9mpjLO9jnnfbAeq1y-DC08SBUt2xBnYo",
"expires": 1468555582491,
"user": {
"imageURL": "http://app.com/images/null",
"email": "mail@gmail.com",
"firstname": "mister",
"lastname": "lalalala",
"password": "$2a$10$VDGtxjz9w7f170Wc66OJi.1T",
"id": "c19349c4044d34333",
"language": "EN",
"createdAt": "2015-07-07T14:15:54.000Z",
"updatedAt": "2016-05-06T08:25:20.000Z"
}
}
我可以像这样访问“令牌”:
String json = new String(bytes);
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("debug", "TOKEN : " + jsonObj.getString("token"));
但我不知道如何获得用户的电子邮件或名字,例如。
我该怎么做
感谢使用获取“user”对象,然后对结果JSONObject使用getString(字符串名称)
:
String json = new String(bytes);
JSONObject jsonObj = null;
JSONObject userObj = null;
try {
jsonObj = new JSONObject(json);
userObj = jsonObj.getJSONObject("user");
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("debug", "firstname : " + userObj.getString("firstname"));
p.S
如果您想真正正确地反序列化JSON,我建议您开始使用。通过这种方式,您可以将JSON字符串转换为可以与之交互的实际Java对象。例如,使用JSON库:
回应类别:
public class Response {
public String token;
public Long expires;
public User user;
}
用户类别:
public class User {
public String imageURL;
public String email;
public String firstname;
public String lastname;
public String password;
public String id;
public String language;
public Date createdAt;
public Date updatedAt;
}
反序列化代码:
Gson gson = new Gson();
Response response = gson.fromJson( jsonString, Response.class );
User user = response.user;
Log.d("debug", "firstname : " + user.firstname;
简单地说
String email = jsonObj.getJSONObject("user").getString("email");
您必须使用
JsonObject
String yourJson = new String(your json data);
JSONObject userObj = null;
try {
JSONObject jsonObj = new JSONObject(yourJson);
userObj = jsonObj.getJSONObject("user");
String email = userObj.getString("email");
String firstname = userObj.getString("firstname");
System.out.println("Email-> " + email);
System.out.println("Firstname-> " + firstname);
} catch (JSONException e) {
e.printStackTrace();
}
或者你可以使用
android studio中的插件
将json复制并粘贴到文件中,它将自动创建其getter和setter
Java
当您获得Json字符串时,只需使用[Gson][2]
对象将其解析为模型类,并获得如下所示的所有值
Gson gson = new Gson();
UserModel obj = gson.fromJson(yourJson,UserModel.class);
String email = obj.getUser().getEmail();
String firstName = obj.getUser().getFirstname();
String token = obj.getToken();
可能重复的
Gson gson = new Gson();
UserModel obj = gson.fromJson(yourJson,UserModel.class);
String email = obj.getUser().getEmail();
String firstName = obj.getUser().getFirstname();
String token = obj.getToken();