2

Есть такой JSON, который отдает сервер, меня смущает property, внутри properties, особенно значения @key, @value, как правильно разобрать эти properties?

введите сюда описание изображения

пока properties было просто строкой, все было просто:

был класс User:

public class User {

    @SerializedName("username")
    String username;
    @SerializedName("name")
    String name;
    @SerializedName("email")
    String email;
    @SerializedName("properties")
    String properties;

    public User(String username, String name, String email, String properties) {
        this.username = username;
        this.name = name;
        this.email = email;
        this.properties = properties;
    }



    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 getEmail() {
        return email;
    }

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

    public String getProperties() {
        return properties;
    }

    public void setProperties(String properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return username + " (" + name + ")";
    }

}

Интерфейс с API:

public interface MessengerApi {
    @GET("users")
    Observable<List<User>> users(@Header("Authorization") String auth,
                                 @Header("Content-type") String contentType,
                                 @Header("Accept") String accept);
}

Метод в котором получаю данные:

private void getUsers() {
    String credentials = "admin" + ":" + "admin";
    final String basic =
            "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
    String contentType = "application/json";
    String accept = "application/json";
    Subscription subscription = App.service.users(basic, contentType, accept)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(users -> {
              Log.e("Users", users.toString());
                contacts.addAll(users);
                for (int i = 0; i < contacts.size(); i++) {
              Log.e("Users in allUsers", String.valueOf(contacts.get(i)));
                }

                contactAdapter.notifyDataSetChanged();
            }, throwable -> {

            });

    addSubscription(subscription);
}

2 ответа 2

3

попробуй так

public class User {
@SerializedName("properties")
private Properties properties;
...
}

public class Properties {
@SerializedName("property")
 private Property property;
...
}


public class Property {
@SerializedName("@key")
 private String key;
@SerializedName("@value")
 private String value;
...
}
2

Добавила еще класс Users:

public class Users {

    @SerializedName("user")
    List<User> user;
    // getters and setters 
}

А так же:

public class User {
@SerializedName("properties")
private Properties properties;
...
}

public class Properties {
@SerializedName("property")
 private Property property;
...
}


public class Property {
@SerializedName("@key")
 private String key;
@SerializedName("@value")
 private String value;
...
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.