如何从json响应中获取子对象

时间:2015-11-13 18:10:59

标签: java android json retrofit

我试图从json响应中检索内部对象,我的json pojo看起来像这样:

public class Pojo {
    private String token;
    private User user;

    public Pojo()
    {}
    public Pojo(String username, String password,User user) {
        user.setUsername(username);
        user.setPassword(password);
        this.user = user;
    }

    public String getToken() {return token;}
    public void setToken(String token) {this.token = token;}
    public User getUser() {return user;}
    public void setUser(User user) {this.user = user;}

我的innerobjct用户看起来像这样:

    public class User {

            private String username

;
        private String name;
        private String phone;
        private String email;
        private String password;
        private String is_Active;
}

与他们的主持人和吸气剂

这是我的登录代码:

public void onLogin(View view){

        final ProgressDialog dialog = ProgressDialog.show(this, "", "loading...");
        EndpointInterface loginService = ServiceAuthGenerator.createService(EndpointInterface.class);
        Password = tv_Password.getText().toString();
        Username = tv_Username.getText().toString();
        User usr = new User();
        Pojo user = new Pojo(Username,Password,usr);
        Call<Pojo> call = loginService.getToken(usr);
        call.enqueue(new Callback<Pojo>() {
            @Override
            public void onResponse(Response<Pojo> response, Retrofit retrofit) {
                dialog.dismiss();
                if (response.isSuccess()) {
                    Pojo user = response.body();
                    if(user.getUser().getIs_Active()=="True") {
                        Intent intent = new Intent(getApplicationContext(), MainMenu.class);
                        startActivity(intent);
                    }
                    else{
                        Toast.makeText(getApplicationContext(), "Wrong User or Password", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            @Override
            public void onFailure(Throwable t) {
                dialog.dismiss();
                Toast.makeText(getApplicationContext(), "Error Conection", Toast.LENGTH_SHORT).show();
            }
        });
    }

响应如下:

{
"token":"tokengfsgfds"
"user":{
   "username":"exmplename"
   "email":"@gomail.com"
   "is_active":"True"
    }
}

我可以检索令牌,但是当我尝试从用户内部对象获取变量时,我的应用程序失败了。谢谢!

1 个答案:

答案 0 :(得分:1)

这只是使用Gson库的模型类的镜像代码

Pojo.java

import com.google.gson.annotations.SerializedName;

public class Pojo {

@SerializedName("token")
private String token;

@SerializedName("user")
private User user;

public Pojo(String username, String password,User user) {
    // TODO Auto-generated constructor stub
    user.setUsername(username);
    user.setPassword(password);
    this.token = "tokengfsgfds";
    this.user = user;
}

public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}
}

将用户从Pojo中取出并制作另一个课程User.java

import com.google.gson.annotations.SerializedName;


public class User {

@SerializedName("username")
private String username;

@SerializedName("name")
private String name;

@SerializedName("phone")
private String phone;

@SerializedName("email")
private String email;

@SerializedName("password")
private String password;

@SerializedName("is_Active")
private boolean is_active;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public boolean isIs_active() {
    return is_active;
}

public void setIs_active(boolean is_active) {
    this.is_active = is_active;
}
}

我使用Gson但输出应该是相同的

import com.google.gson.Gson;

public class TestTwo {
public static void main(String[] args) {
    User user = new User();
    user.setEmail("someone@gmailcom");
    user.setIs_active(true);
    user.setName("Cristian");
    user.setPassword("Cam");
    user.setPhone("1234123441");
    user.setUsername("cam.cri");

    Pojo pojo = new Pojo("cam.cri", "Cam", user);

    String result = (new Gson()).toJson(pojo);
    System.out.println(""+result);

    Pojo pojo2 = (new Gson()).fromJson(result, Pojo.class);

    System.out.println("Token: \t"+pojo2.getToken());
    System.out.println("email: \t"+pojo2.getUser().getEmail());
    System.out.println("is_active: \t"+pojo2.getUser().isIs_active());
    System.out.println("Name: \t"+pojo2.getUser().getName());
    System.out.println("Password: \t"+pojo2.getUser().getPassword());
    System.out.println("phone: \t"+pojo2.getUser().getPhone());
    System.out.println("Username: \t"+pojo2.getUser().getUsername());

}
}

输出

{
  "token": "tokengfsgfds",
  "user": {
"username": "cam.cri",
"name": "Cristian",
"phone": "1234123441",
"email": "someone@gmailcom",
"password": "Cam",
"is_Active": true
  }
    }

输出

Token:  tokengfsgfds
email:      someone@gmailcom
is_active:  true
Name:       Cristian
Password:   Cam
phone:      1234123441
Username:   cam.cri