0

I'm currently receiving a JSON response from a server that I'm attempting to parse. Getting the actual properties has been simple, but I'm having trouble parsing an object within the response into an array that I can then parse into a table for display.

{
    "response": "User exists",
    "data": {
        "id": 5,
        "list": 7,
        "first_name": "Aaron",
        "last_name": "Test",
        "email": "testing@testdomaij.com",
        "custom_fields": {
            "Is this a question?": "This is an answer",
            "Test Question": "Test answer!"
        },
        "created_at": "Oct 14, 2015"
    }
}

I'm already passing the data object into a POJO and am able to get all of the strings and integers, but I'm not sure how to parse the custom_fields object into an array. The keys and values will be different for every user, and I need to find a way to display them.

My current User object that parses the data is as follows:

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;

public class User implements Parcelable {

    private String id;
    private String list;
    @SerializedName("first_name")
    private String firstName;
    @SerializedName("last_name")
    private String lastName;
    private String email;
    @SerializedName("custom_fields")
    private CustomFields customFields;
    @SerializedName("created_at")
    private String createdAt;

    public User() {

    }

    public User(Parcel source) {
        id = source.readString();
        list = source.readString();
        firstName = source.readString();
        lastName = source.readString();
        email = source.readString();
        createdAt = source.readString();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getList() {
        return list;
    }

    public void setList(String list) {
        this.list = list;
    }

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

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

    public void getCustomFields() {
       // I have no idea how to parse and return as an array
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(list);
        dest.writeString(firstName);
        dest.writeString(lastName);
        dest.writeString(email);
        dest.writeString(createdAt);
    }

    public static Creator<User> CREATOR = new Creator<User>() {

        @Override
        public User createFromParcel(Parcel source) {
            return new User(source);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }

    };
}

How do I go about parsing the keys and values of the custom_fields object and returning them as an array I can use?

aaronhuisinga
  • 299
  • 1
  • 5
  • 16
  • If the order that the key/vals are read doesn't matter, this post might help: http://stackoverflow.com/questions/9151619/java-iterate-over-jsonobject – mjp66 Nov 03 '15 at 22:39
  • Have you considered modifying the setter to actually parse the string by comma and then parse each string by : add the first value to a Key and the second to value of a Map that is actually added to the POJO as a class variable that is not used in the parsing(assuming there is an annotation that will just ignore it)... and then add another method that will return that Map were you then iterate over every key and use key and values to display the question and answer. – N Jay Nov 03 '15 at 23:23

0 Answers0