我的Android应用中有一个像这样的JSON:
onCreate
我可以像这样访问“令牌”:
{
"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"
}
}
但我不知道如何获取用户的电子邮件或名字。 我怎么能这样做?
由于
答案 0 :(得分:4)
使用getJSONObject(String name)获取"用户"对象然后在生成的JSONObject上使用getString(String name)
:
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,我建议您开始使用POJOs。这样,您可以将JSON字符串转换为可以与之交互的实际Java对象。例如,使用JSON库GSON:
响应类:
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;
答案 1 :(得分:1)
简单地说,
String email = jsonObj.getJSONObject("user").getString("email");
答案 2 :(得分:0)
您必须使用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();
}
或者您可以使用
plugin
中的 android studio
将json复制并粘贴到文件中,它将自动创建其getter和setter。
UserModel.Java
public class UserModel {
private String token;
private long expires;
private UserBean user;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public long getExpires() {
return expires;
}
public void setExpires(long expires) {
this.expires = expires;
}
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
public static class UserBean {
private String imageURL;
private String email;
private String firstname;
private String lastname;
private String password;
private String id;
private String language;
private String createdAt;
private String updatedAt;
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
}
当你得到Json String时,只需使用[Gson][2]
对象将其解析为Model类,并获取所有这些值。
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();