I am facing json conversion exception. While i converting json to java object.
Here is my json
[
{
"PrefrenceId":"228f176d-d224-32d7-9bb5-6287a06a68e8",
"UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
"QuestionnaireId":"41f31b11-47f5-3e29-8c88-1a3615c978a7",
"Suggestions":"",
"Explanation":"",
"IsActive":true,
"IsDelete":false,
"DateCreated":"2016-11-01 09:53:00.000",
"DateUpdated":"2016-11-01 09:53:17.000"
},
{
"PrefrenceId":"52a74739-bdd3-33ac-a83f-72f60b1992b5",
"UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
"QuestionnaireId":"8cd5ac8e-89db-3d7b-bb2d-4e6735b245de",
"Suggestions":"",
"Explanation":"",
"IsActive":true,
"IsDelete":false,
"DateCreated":"2016-11-01 09:48:53.000",
"DateUpdated":"2016-11-01 09:53:15.000"
},
{
"PrefrenceId":"ae7fc877-b26a-34d3-a5f3-244c7e777e08",
"UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
"QuestionnaireId":"d3b98cde-111c-30d5-a4c9-412a76b656eb",
"Suggestions":"Camping",
"Explanation":"",
"IsActive":true,
"IsDelete":false,
"DateCreated":"2016-11-01 09:53:02.000",
"DateUpdated":"2016-11-01 09:53:19.000"
},
{
"PrefrenceId":"bcac0da7-31a6-345f-be82-ddff17c29b35",
"UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
"QuestionnaireId":"8fb1bda7-7ec8-3538-8aa8-ff84637764a4",
"Suggestions":"",
"Explanation":"",
"IsActive":true,
"IsDelete":false,
"DateCreated":"2016-11-01 09:53:07.000",
"DateUpdated":"2016-11-01 09:53:22.000"
},
{
"PrefrenceId":"ff46ce3c-70cb-3d25-8dbb-10e9c46d4c2d",
"UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
"QuestionnaireId":"3afffc17-30e4-311f-a0fc-8daa3bda6c98",
"Suggestions":"",
"Explanation":"",
"IsActive":true,
"IsDelete":false,
"DateCreated":"2016-11-01 09:53:05.000",
"DateUpdated":"2016-11-01 09:53:20.000"
}
]
My POJO classes :-
public class SurvivorZAMQuestionList implements Serializable {
public List<SurvivorZAMQuestionnaire> survivorZAMQuestionnaires;
}
public class SurvivorZAMQuestionnaire implements Serializable {
public String Suggestions;
public String PrefrenceId;
public String IsActive;
public String IsDelete;
public String DateCreated;
public String DateUpdated;
public String UserId;
public String QuestionnaireId;
public String Explanation;
}
But when i am converting json response to json it is showing following error:- com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
Can anyone tell me what i am missing in pojo class. Any kind of held should be appreciated.
Your response is proper but your parsing is not proper. So first of all add GSON in your gradle file.
compile 'com.google.code.gson:gson:2.4'
Now use below your code for parsing your response
try {
JSONArray array = new JSONArray("put your response here");
Gson gson = new Gson();
for (int i = 0 ; i <array.length();i++)
{
SurvivorZAMQuestionnaire obj = new SurvivorZAMQuestionnaire();
obj.add(gson.fromJson(array.getJSONObject(i).toString(),SurvivorZAMQuestionnaire.class));
}
} catch (JSONException e) {
e.printStackTrace();
}
Add your obj in list and show it :)
The Error clearly states that the Gson accepts JsonObject not JsonArray. In your case you can put the response JsonArray into a JsonObject with a key for that JsonArray and give that key as annotation in SurvivorZAMQuestionList. By this way you can easily sort this problem.
Hope this is Helpful :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With