shaheen shaheen - 6 months ago 44
JSON Question

parsing JSON array of JSON objects using Jackson throws JSON Mapping Exception, how to fix?

This is my JSON :

[
{
"_id": "574f1840d21303358a03d78b",
"name": "Bank One of Kerala",
"period_from": "2016-03-12T00:00:00.000Z",
"period_to": "2016-03-12T00:00:00.000Z",
"status": true,
"ins": "Get an account at Bank One of Kerala :P",
"fields": [
{
"field": "Account Number",
"key": "acc"
},
{
"field": "IFSC",
"key": "ifsc"
},
{
"field": "Branch",
"key": "branch"
},
{
"field": "Name",
"key": "name"
},
{
"field": "PAN",
"key": "pan"
}
]
},
{
"_id": "574f18e4d21303358a03d78c",
"name": "Bank Two of India",
"period_from": "2016-03-12T00:00:00.000Z",
"period_to": "2016-03-12T00:00:00.000Z",
"status": true,
"ins": "Get an account at Bank Two of India",
"fields": [
{
"field": "Account Number",
"key": "acc"
},
{
"field": "IFSC",
"key": "ifsc"
},
{
"field": "Branch",
"key": "branch"
},
{
"field": "Name",
"key": "name"
}
]
}
]


This is my model class:

ShreyPojo.class

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"_id",
"name",
"period_from",
"period_to",
"status",
"ins",
"fields"
})
public class ShreyPojo
{
@JsonProperty("_id")
private String id;
@JsonProperty("name")
private String name;
@JsonProperty("period_from")
private String periodFrom;
@JsonProperty("period_to")
private String periodTo;
@JsonProperty("status")
private Boolean status;
@JsonProperty("ins")
private String ins;
@JsonProperty("fields")
private List<Field> fields = new ArrayList<Field>();
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
*
* @return
* The id
*/
@JsonProperty("_id")
public String getId() {
return id;
}

/**
*
* @param id
* The _id
*/
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}

/**
*
* @return
* The name
*/
@JsonProperty("name")
public String getName() {
return name;
}

/**
*
* @param name
* The name
*/
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

/**
*
* @return
* The periodFrom
*/
@JsonProperty("period_from")
public String getPeriodFrom() {
return periodFrom;
}

/**
*
* @param periodFrom
* The period_from
*/
@JsonProperty("period_from")
public void setPeriodFrom(String periodFrom) {
this.periodFrom = periodFrom;
}

/**
*
* @return
* The periodTo
*/
@JsonProperty("period_to")
public String getPeriodTo() {
return periodTo;
}

/**
*
* @param periodTo
* The period_to
*/
@JsonProperty("period_to")
public void setPeriodTo(String periodTo) {
this.periodTo = periodTo;
}

/**
*
* @return
* The status
*/
@JsonProperty("status")
public Boolean getStatus() {
return status;
}

/**
*
* @param status
* The status
*/
@JsonProperty("status")
public void setStatus(Boolean status) {
this.status = status;
}

/**
*
* @return
* The ins
*/
@JsonProperty("ins")
public String getIns() {
return ins;
}

/**
*
* @param ins
* The ins
*/
@JsonProperty("ins")
public void setIns(String ins) {
this.ins = ins;
}

/**
*
* @return
* The fields
*/
@JsonProperty("fields")
public List<Field> getFields() {
return fields;
}

/**
*
* @param fields
* The fields
*/
@JsonProperty("fields")
public void setFields(List<Field> fields) {
this.fields = fields;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}


Field.class

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"field",
"key"
})

public class Field
{

@JsonProperty("field")
private String field;
@JsonProperty("key")
private String key;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
*
* @return
* The field
*/
@JsonProperty("field")
public String getField() {
return field;
}

/**
*
* @param field
* The field
*/
@JsonProperty("field")
public void setField(String field) {
this.field = field;
}

/**
*
* @return
* The key
*/
@JsonProperty("key")
public String getKey() {
return key;
}

/**
*
* @param key
* The key
*/
@JsonProperty("key")
public void setKey(String key) {
this.key = key;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}


And, now when I try to parse the JSON,

ShreyPojo obj = mapper.readValue(res,ShreyPojo.class); //res is the JSON string
Log.w("JACKSON",""+obj.getName());


I get the exception :

D/OkHttp: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.shreybank.shrey.pojo.ShreyPojo out of START_ARRAY token
D/OkHttp: at [Source: [{"_id":"574f1840d21303358a03d78b","name":"Bank One of Kerala","period_from":"2016-03-12T00:00:00.000Z","period_to":"2016-03-12T00:00:00.000Z","status":true,"ins":"Get an account at Bank One of Kerala :P","fields":[{"field":"Account Number","key":"acc"},{"field":"IFSC","key":"ifsc"},{"field":"Branch","key":"branch"},{"field":"Name","key":"name"},{"field":"PAN","key":"pan"}]},{"_id":"574f18e4d21303358a03d78c","name":"Bank Two of India","period_from":"2016-03-12T00:00:00.000Z","period_to":"2016-03-12T00:00:00.000Z","status":true,"ins":"Get an account at Bank Two of India","fields":[{"field":"Account Number","key":"acc"},{"field":"IFSC","key":"ifsc"},{"field":"Branch","key":"branch"},{"field":"Name","key":"name"}]}]; line: 1, column: 1]
D/OkHttp: at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:873)
D/OkHttp: at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:869)
D/OkHttp: at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1293)
D/OkHttp: at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:159)
D/OkHttp: at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:135)
D/OkHttp: at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
D/OkHttp: at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2779)
D/OkHttp: at com.shreybank.shrey.activities.BankDetailsActivity$1.onResponse(BankDetailsActivity.java:77)
D/OkHttp: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
D/OkHttp: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
D/OkHttp: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
D/OkHttp: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
D/OkHttp: at java.lang.Thread.run(Thread.java:818)


Since similar questions exist, I tried them in case I made a common mistake, since this one is not same, how to fix this?

Answer

Try below code.

 try {

       JSONArray  jsonArray = new JSONArray(res);
       List<ShreyPojo> pojoList = new ArrayList<>();

       for(int i = 0; i < jsonArray.length(); i++)
       {
          //JSONObject jsonObject = jsonArray.getJSONObject(i);
            String jsonObject = jsonArray.getString(i);

          ShreyPojo obj = mapper.readValue(jsonObject,ShreyPojo.class); 

           pojoList.add(obj);
       }

   // Use ShreyPojo objects as per requirement

 } catch (Exception e)
 {
    Log.w("Exception = ","" + e.toString());
 }