All web application on android uses either JSON or XML for receiving data from Android.This is a function which can be used to convert datatable to JSON object.Then it can be give as response.
The input parameter is the dataTable andthe rootNodeName
username,password,name,address,emailid,phoneno
are the Column names in datatable
The output will be
Here registration is the rootNodeName
public string DataTabletoString(DataTable dtData,String RootNodeName)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return "{ \"" + RootNodeName + "\" : " + serializer.Serialize(rows) + " }";
}
The input parameter is the dataTable andthe rootNodeName
username,password,name,address,emailid,phoneno
are the Column names in datatable
The output will be
{ "registration" : [{"username":"user1","password":"pswd","name":"gop","address":"gop","emailid":"emp1@gmail.com","phoneno":1234567},{"username":"user2","password":"pswd","name":"emp2","address":"address2","emailid":"emp2@gmail.com","phoneno":1234567},{"username":"user3","password":"pswd","name":"emp3","address":"address3","emailid":"emp3@gmail.com","phoneno":134234}] }
Here registration is the rootNodeName
No comments:
Post a Comment