2

I retrieve a JSON result from an API :

[{
"oid": "axd7wtlk6xd2fbwlc5wk",
"id": "aazzzza",
"name": "aazzaa",
"logo": {
"type": 0,
"data": "iVB.............5CYII="
},
"timestamp": 1438608571013,
"email": "[email protected]",
"modified": "test",
"url": "http://www.azzaa.net"
},
{
"oid": "quj3dzygfwygl5uxsbxk",
"name": "KZZZ",
"modified": "test",
"timestamp": 1438854099511,
"id": "kess"
},...]

but when I try to map to a customer object I get the error Expected a string but was BEGIN_OBJECT :

    response = webService.RequestGet(url, header);

    result = null;
    try {
        result = new JSONArray(response);
        Utils.LogWarning(response);
    } catch (JSONException e) {
        Utils.LogError("Could not load json response", e);
    }

    Type customerType = new TypeToken<Collection<Customer>>() {
    }.getType();
    ArrayList<Customer> alCustomers = null;
    alCustomers = new Gson().fromJson(result.toString(), customerType);

Here is my Customer class :

public class Customer implements Serializable {
    private String id = "";
    private String name = "";
    private String email = "";
    private String url = "";
    private String address = "";
    private String stamp = "";
    //private transient String logo = "";
    private long timestamp = 0L;
    private String modified = "";
    ...

}

I have been through a lot of answers regarding this problem that I have also for other types of objects, but I can't find a working solution.

5
  • 2
    In your Customer class, is logo field a String? Commented Aug 18, 2015 at 9:54
  • show your customer class Commented Aug 18, 2015 at 9:57
  • Please match your Customer class properties type with the type you have in JSON. Commented Aug 18, 2015 at 9:57
  • logo is a field String but I added the transient keyword as I will store the path and not the image Commented Aug 18, 2015 at 10:04
  • 1
    logo is of type object. convert your class property to object and then keep only path. Commented Aug 18, 2015 at 10:16

1 Answer 1

2

Create a modal with the values of JSON result like

    public class Customer {
        private String oid;
        private String id;
        private String name;
        private String timestamp;
        private String email;
        private String modified;
        private String url;

        public String getOid() {
            return oid;
        }

        public void setOid(String oid) {
            this.oid = oid;
        }

        public String getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

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

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getEmail() {
            return email;
        }

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

        public String getModified() {
            return modified;
        }

        public void setModified(String modified) {
            this.modified = modified;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public Logo getLogo() {
            return logo;
        }

        public void setLogo(Logo logo) {
            this.logo = logo;
        }

        private Logo logo;
    }

    public class Logo {
        private int type;

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        private String data;
    }

  Gson gson = new Gson();
 Type listType = new TypeToken<List<Customer>>(){}.getType();
List<Customer> customer= (List<Customer>) gson.fromJson(jsonOutput, listType);
Sign up to request clarification or add additional context in comments.

1 Comment

if a property is present in a JSON response, it must be present in the model ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.