Android : Better Approach to Parse a common Json and insert it into DB

on Thursday, July 31, 2014


I have two webservices which return similar type of data like :


Data1:



{
"FailedValidations": null,
"IsSuccess": true,
"Message": null,
"Result": [
{
"IsMandatory": true,
"ModuleCode": 23,
"ModuleID": 37,
"Name": "Availability Index",
"ParentModuleID": null,
"Sequence": 0,
"UserID": 0,
"UserRoleID": 0
}]}


Data 2:



{
"FailedValidations": null,
"IsSuccess": true,
"Message": null,
"Result": [
{
"ModuleCode": 23,
"ModuleID": 37,
"Name": "Availability Index",
"Questions": [
{
"DealerTypes": null,
"IsActive": true,
"ModuleCode": 19,
"ModuleID": 33,
"Options": [
{
"IsActive": true,
"OptionValue": "Yes",
"Sequence": 1,
"SurveyOptionID": 45581,
"SurveyQuestionID": 54751
},
{
"IsActive": true,
"OptionValue": "No",
"Sequence": 2,
"SurveyOptionID": 45582,
"SurveyQuestionID": 54751
}
],
"Sequence": 0
}


what i done is , i create single DTO class to parse these both data by using GSON library . like :



private int UserID;
private boolean IsMandatory;
private int Sequence;
private int ModuleID;
private int ModuleCode;
private String ModuleIcon;
private boolean isQuestionType;
private boolean isStoreWise;
private String ParentModuleID;
private String UserRoleID;
private String Name;
private ArrayList<Question> Questions;


and insert theses data in data1 and data2 tables in DB. The above approach works fine but i just want to know : 1.Is this the best approach to parse two similar(almost) json. 2.Or If i create two DTO for each json data is the best approach.


What is above two is best approach and why?? or is there any other way to do this(besides manual parsing).


0 comments:

Post a Comment