JSON conversion exception of Android Java object
•
Android
I face JSON conversion exception. When I convert JSON to Java object
This 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 courses:-
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;
}
However, when I convert the JSON response to JSON, it displays the following error: - com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expecting begin_ Object, but in line 1 and column 2, it is begin_ ARRAY
Who can tell me what I lack in POJO class. Any form of holding should be appreciated
resolvent:
Your response is correct, but your parsing is incorrect. Therefore, first add gson to your gradle file
Compile 'com. Google. Code. Gson: gson: 2.4'
Now, use the following code to parse the 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 to the list and display it:)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码