Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to get array json value in asmx pagem Like this

{
        "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"
                }]
        }]
}


What I have tried:

I make a webservice in asp.net asmx pagem so i need array type Json return,
how can i do that,
Posted
Updated 27-Jun-18 17:15pm

1 solution

You can use the JavaScriptSerializer class to transform a C# class/object into JSON format just like what is demonstrated here: Return JSON Response From Web Service In ASP.NET

Alternatively, you could use Newtonsoft's SerializeObject: Creating Web Services In .NET To Return XML And JSON Data

To transform DataTable to JSON, see this discussion: Convert DataTable to JSON
 
Share this answer
 
v3
Comments
Rohit Sharma706 28-Jun-18 14:23pm    
Thanks for ur valuable answer, but i already applied as ur given link, But i want as above post, 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
Vincent Maverick Durano 28-Jun-18 20:36pm    
You didn't mention that you want to convert DataTable to JSON. Please refer this discussion on how to transform a DataTable to JSON: https://stackoverflow.com/questions/17398019/convert-datatable-to-json-in-c-sharp
Vincent Maverick Durano 28-Jun-18 20:38pm    
I've updated the solution to include a link for converting DataTable to JSON. The idea is still to use JavaScriptSerializer or Newtonsoft SerializeObject.
Member 14558079 30-Nov-19 13:15pm    
is work fine or not

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900