Rohit Sharma

Rohit Sharma

  • NA
  • 100
  • 44.4k

How to get Json array format as below

Jun 28 2018 1:27 PM
{
"Status": 1,
"message": "",
"CityList": [{
"CityId": "1",
"CityName": "Rajkot",
"AreaList": [{
"AreaId": "1",
"AreaName": "Airport Road"
}, {
"AreaId": "2",
"AreaName": "Bus Stand"
}]
}, {
"CityId": "2",
"CityName": "Ahmedabad",
"AreaList": [{
"AreaId": "1",
"AreaName": "Airport Road"
}, {
"AreaId": "2",
"AreaName": "Bus Stand"
}]
}]
}
--------------------------------------------
I have DataTable of city and Area, how can i do the code to get as above formate,
---------------------------------
DataSet dsCS = Objbl.Fill_Grid("GetCountryandState", new string[] //Objbl.Fill_Grid("GetArea", new string[]
{
"View"
});
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(GetJSON(dsCS));
-------------------------------------------------------------------------------
public string GetJSON(DataSet ds)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ArrayList root = new ArrayList();
List<Dictionary<string, object>> table;
Dictionary<string, object> data;
foreach (DataTable dt in ds.Tables)
{
table = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
data = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
data.Add(col.ColumnName, dr[col]);
}
table.Add(data);
}
root.Add(table);
}
return serializer.Serialize(root);
}
-----------------------------------------------------------------------------
I am using this code but unable to get the above format. So please help me

Answers (1)