3
   public ActionResult About()
    {
        List<Stores> listStores = new List<Stores>();
        listStores = this.GetResults("param");
        return Json(listStores, "Stores", JsonRequestBehavior.AllowGet);
    }

Using the above code I am able to get the below result

[{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555","email":"[email protected]","geo":{"latitude":"12.9876","longitude":"122.376237"}},

{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454","email":"[email protected]","geo":{"latitude":"12.9876","longitude":"122.376237"}},

How would I able to get the result in below format?

{

"stores" : [
{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555","email":"[email protected]",
"geo":{"latitude":"12.9876","longitude":"122.376237"}},

{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454","email":"[email protected]","geo":{"latitude":"12.9876","longitude":"122.376237"}} ] 
}

I would like to have the stores at the beginning of the data.

Please help me in this regard.

4 Answers 4

5

You will need to create an object that contains the stores within a property named stores:

public ActionResult About()
{
    var result = new { stores = this.GetResults("param") };
    return Json(result, "Stores", JsonRequestBehavior.AllowGet);
}

I've used an anonymous type for simplicity here, if this result type were required in multiple places you may consider creating a 'proper' class for it.

Sign up to request clarification or add additional context in comments.

Comments

1

JavaScriptSerializer can be found from namespace System.Web.Script.Serialization

var ser = new JavaScriptSerializer();
var jsonStores = ser.Serialize(stores);

return Json(new { stores: jsonStores }, "Stores", JsonRequestBehavior.AllowGet);

Comments

1

if you want to send object to client side as Json format like Data-table,List,Dictionary etc. then need to override jsonResult and ExecuteResult

other wise use linq format to return data like

using JSON.NET(must need to use override jsonResult and ExecuteResult )

  DataTable dt = new DataTable();//some data in table
   return json("data",JsonConvert.SerializeObject(dt))

other option using linq

var Qry = (from d in dt.AsEnumerable()
                               select new
                               {
                                   value = d.Field<int>("appSearchID"),
                                   text = d.Field<string>("appSaveSearchName"),
                                   type = d.Field<int>("appSearchTypeStatus")
                               });
                     return json("Data", Qry);

override methods

 protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            try
            {
                    return new JsonNetResult
                    {
                        Data = data,
                        ContentType = contentType,
                        ContentEncoding = contentEncoding,
                        JsonRequestBehavior = behavior,
                        MaxJsonLength = int.MaxValue
                    };

            }
            catch (Exception)
            {
                throw;
            }
        }

    public class JsonNetResult : JsonResult
        {
           public override void ExecuteResult(ControllerContext context)
            {
                try
                {
                HttpResponseBase response = context.HttpContext.Response;
                    response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
                    if (this.ContentEncoding != null)
                        response.ContentEncoding = this.ContentEncoding;
                    if (this.Data == null)
                        return;
                    using (StringWriter sw = new StringWriter())
                    {
                        response.Write(this.Data);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

1 Comment

Although your answer might fix the problem, try to explain WHY it fixes the problem. Point out some things that you've changed / fixed to make it work. This will help OP to understand why your answer fixes his problem.
-1
public class StoresViewModel{
    public List<Stores> stores {get;set;}
}


public ActionResult About()
{
        List<Stores> listStores = new List<Stores>();
        listStores = this.GetResults("param");
        StoresViewModelmodel = new StoresViewModel(){
            stores = listStores;
        }
        return Json(model, JsonRequestBehavior.AllowGet);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.