REST gives JSONObject output when it has only one entry
Posted: Tue May 07, 2013 9:12 pm
I am using resttemplate with Gson to deserialize the json data.
I am using List<Object> wherever it can send JSONArray output.
But the problem is whenever there is single entry/item it gives output in the form of JSONObject.
I such cases my deserializer gives exception as "Expected BEGIN_ARRAY but was BEGIN_OBJECT".
Please tell me how to solve this problem. Is there any good rest client library to use with android which can handle such kind of JSON data.
JSONArray example (for more than one item)
----------------------
{
"product":
[
{
"id": "11",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"manufacturer": "Brazos Legends",
"defaultSku":
{
"id": "11",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"retailPrice":
{
"amount": "3.99",
"currency": "USD"
}
},
"productOptions": null
},
{
"id": "12",
"name": "Cafe Louisiane Sweet Cajun Blackening Sauce",
"activeStartDate": "2013-05-07T14:09:39.198+05:30",
"manufacturer": "Garden Row",
"defaultSku":
{
"id": "12",
"activeStartDate": "2013-05-07T14:09:39.198+05:30",
"name": "Cafe Louisiane Sweet Cajun Blackening Sauce",
"retailPrice":
{
"amount": "4.99",
"currency": "USD"
}
},
"productOptions": null
}
]
}
JSONObject output (for single item)
----------------------
{
"product":
{
"id": "11",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"manufacturer": "Brazos Legends",
"defaultSku":
{
"id": "11",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"retailPrice":
{
"amount": "3.99",
"currency": "USD"
}
},
"productOptions": null
}
}
//note:- here square brackets removed for single item
I am using List<Object> wherever it can send JSONArray output.
But the problem is whenever there is single entry/item it gives output in the form of JSONObject.
I such cases my deserializer gives exception as "Expected BEGIN_ARRAY but was BEGIN_OBJECT".
Please tell me how to solve this problem. Is there any good rest client library to use with android which can handle such kind of JSON data.
JSONArray example (for more than one item)
----------------------
{
"product":
[
{
"id": "11",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"manufacturer": "Brazos Legends",
"defaultSku":
{
"id": "11",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"retailPrice":
{
"amount": "3.99",
"currency": "USD"
}
},
"productOptions": null
},
{
"id": "12",
"name": "Cafe Louisiane Sweet Cajun Blackening Sauce",
"activeStartDate": "2013-05-07T14:09:39.198+05:30",
"manufacturer": "Garden Row",
"defaultSku":
{
"id": "12",
"activeStartDate": "2013-05-07T14:09:39.198+05:30",
"name": "Cafe Louisiane Sweet Cajun Blackening Sauce",
"retailPrice":
{
"amount": "4.99",
"currency": "USD"
}
},
"productOptions": null
}
]
}
JSONObject output (for single item)
----------------------
{
"product":
{
"id": "11",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"manufacturer": "Brazos Legends",
"defaultSku":
{
"id": "11",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"retailPrice":
{
"amount": "3.99",
"currency": "USD"
}
},
"productOptions": null
}
}
//note:- here square brackets removed for single item