title: How to return JSON Array in ASP.NET Web API C#
body: <p>I want to return (display in the web browser) json array in below format.</p>
  
  <pre><code>{
      "RainfallAreaAVG": [
      {
          "AreaBbsID": "18",
          "DistCount": "1",
          "SubDistCount": "2",
          "Amount": "14",
          "Hail": "14",
          "ArealDetails": [
                  {
                      "DistBbsID": "101",
                      "SubDistCount": "2",
                      "Amount": "14",
                      "Hail": "14",
                      "SubDistCount": "2",
                      "DistDetails": [
                          {
                              "SubDistBbsID": "101",
                              "Amount": "14",
                              "Hail": "2",
                              "Date": "2011-06-13"
                          },
                          {
                              "SubDistBbsID": "102",
                              "Amount": "10",
                              "Hail": "0",
                              "Date": "2011-06-13"
                          }
                      ]
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>I am using asp.net web API (MVC) in c# and Entity Framework 5.0, ADO.Net Entity Data Model as my model.</p>
  
  <p>I'm using stored procedure to get data from sql server DB:</p>
  
  <p><a href="https://i.stack.imgur.com/fbm4p.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fbm4p.png" alt="enter image description here"></a></p>
  
  <p>At present I'm using code below in my controller</p>
  
  <pre><code>namespace RainfallService.Controllers
  {
      public class DistAVGController : ApiController
      {
  
          [HttpGet]
          public List&lt;SP_GetRainfallByDistDateAVG_Result&gt; GetRainfall(string distBbsID, string entryDate)
          {
              using (var db = new Farmer_WebEntities())
              {
                  var rainfalls = db.SP_GetRainfallByDistDateAVG(distBbsID, entryDate).ToList();
                  return rainfalls;
              }
          }
  
          [HttpGet]
          public List&lt;SP_GetRainfallByDistDateAVGDetails_Result&gt; GetRainfall(string distBbsID, string entryDate,string type)
          {
              using (var db = new Farmer_WebEntities())
              {
                  var rainfalls = db.SP_GetRainfallByDistDateAVGDetails(distBbsID, entryDate).ToList();
                  return rainfalls;
              }
          }
  
      }
  }
  </code></pre>
  
  <p>And my output is like below which I don't want.</p>
  
  <p><a href="https://i.stack.imgur.com/WbY8X.png" rel="noreferrer"><img src="https://i.stack.imgur.com/WbY8X.png" alt="For Average"></a>
  <a href="https://i.stack.imgur.com/DM0vV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DM0vV.png" alt="For Details"></a></p>
  
  <p>ADO.Net Entity Data Model using like below
  <a href="https://i.stack.imgur.com/t3X48.png" rel="noreferrer"><img src="https://i.stack.imgur.com/t3X48.png" alt="enter image description here"></a></p>
  
  <p>Model Classes I'm using</p>
  
  <pre><code>namespace RainfallService
  {
      using System;
  
      public partial class SP_GetRainfallByDistDateAVG_Result
      {
          public string AreaBbsId { get; set; }
          public string DistBbsID { get; set; }
          public Nullable&lt;int&gt; SubDistCount { get; set; }
          public Nullable&lt;decimal&gt; Amount { get; set; }
          public Nullable&lt;int&gt; Hail { get; set; }
      }
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>namespace RainfallService
  {
      using System;
  
      public partial class SP_GetRainfallByDistDateAVGDetails_Result
      {
          public string AreaBbsId { get; set; }
          public string DistBbsID { get; set; }
          public string SubDistBbsId { get; set; }
          public Nullable&lt;decimal&gt; Amount { get; set; }
          public Nullable&lt;int&gt; Hail { get; set; }
      }
  }
  </code></pre>
  
  <p>My WebApiConfig.cs as below</p>
  
  <pre><code>namespace RainfallService
  {
      public class WebApiConfig
      {       
          public static void Register(HttpConfiguration config)
          {
              // Web API configuration and services
  
              EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
              config.EnableCors(cors);
  
              // Web API routes
              config.MapHttpAttributeRoutes();
  
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
  
              config.Formatters.Remove(config.Formatters.XmlFormatter);
              //config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
              //var jsonFormatter = config.Formatters.OfType&lt;JsonMediaTypeFormatter&gt;().First();
              //jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
          }
      }
  }
  </code></pre>
  
  <p>Can anybody help me please???</p>