Wednesday, January 18, 2012

Return multiple object using JSON in Asp.net MVC

Here are we going to discuss what are the differnt ways to send json response to browser.

Note: we are assuming that json2. js file is already added into html page  you can use following url to download json2.js file or you can directly add this link in your page .

http://www.JSON.org/json2.js
<script type="text/javascript" src=http://www.JSON.org/json2.js />

1. It  is straight forward to retrun a single json object using following statement:

public JsonResult getResult()
{
        // Create an object of any kind
        Report objReport= new Report();
  
retun Json( objReport);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object


}


2. It straight forward to retrun a Multiple object using json  byfollowing statement:

public JsonResult getResult()
{
        // Create an object of any kind
        Report actualReport= new Report();
        Report scheduledReport= new Report();
  
retun Json( new { ActualReport=actualReport,ScheduledReport=scheduledReport);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object
 var actualReport= JsonObject.ActualReport;
var scheduledReport= JsonObject.ScheduledReport;

}

3. It straight forward to retrun a Dynamically Added Multiple object using json  by following statement:

a) if we are sending list of object we will be able to access these using Indexes
public JsonResult getResult(int numberOfReports)
{
        // Create an object of any kind
      List<Report> reportList= new List<Report>();

        Report report= null;
        for(int i=0;i<numberOfReports;i++){
                 report= new Report(); 
                // set properties to report Object 
                 reportList.Add(report);
        }
       
  
retun Json( reportList);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object
// use JsonObject like
//alert(JsonObject[0]  );

}

b) if we are sending list of object we will be able to access these using Indexes
public JsonResult getResult(int numberOfReports)
{
        // Create an object of any kind
      Dictionary<string,Report> reportList= new Dictionay<string,Report>();

        Report report= null;
        for(int i=0;i<numberOfReports;i++){
                 if(i%2==0){                
                 report= new Report(); 
                // set properties to report Object 
                 reportList.Add("obj"+i.ToString(),report);
               }
               elseif(i%2!=0)
              {
                        report= new Report(); 
                      // set properties to report Object 
                       reportList.Add("obj"+ i.ToString(),report);
               }
        }
       
  
retun Json( reportList);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object
// use JsonObject like
//alert(JsonObject.obj1  );
//alert(JsonObject.obj2  );
// and so on

}

I hope it will help you how can me send JsonResult back to browser.

Happy Living...
Happy Concepts...
Happy Programming...

No comments:

Post a Comment