3
public ActionResult About()
{
List listStores = new List();
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":"abc@ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"}},
 {"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454",
"email":"nfnf@ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"}},

how would I able to get the result in below format? Would need stores at the beginning of the result.

{
"stores" : [
{"id":"1","name":"Store1","cust_name":"custname1","telephone":"1233455555",
     "email":"abc@ac.com",
     "geo":{"latitude":"12.9876","longitude":"122.376237"}},
{"id":"2","name":"Store2","cust_name":"custname2","telephone":"1556454",
     "email":"nfnf@ac.com","geo":{"latitude":"12.9876","longitude":"122.376237"
}} ] }
gideon
  • 19,329
  • 11
  • 72
  • 113
pili
  • 795
  • 2
  • 10
  • 24

1 Answers1

8

Try

return Json(new { stores = listStores }, JsonRequestBehavior.AllowGet);

In the above statement, you're creating a new object with a property named "stores", which is then populated with the array of items from the list.

You could also use a class, something defined like so:

[DataContract]
public class StoreListJsonDTO
{
    [DataMember(Name = "stores")]
    public List Stores { get; set; }

    public StoreListJsonDTO(List storeList)
    {
        this.Stores = storeList;
    }
}

Then in your code, you'd do:

var result = new StoreListJsonDTO(listStores);
return Json(result, JsonRequestBehavior.AllowGet);
Chris
  • 27,596
  • 25
  • 124
  • 225
  • lets say i want to change the name of the "stores" to some thing else like serialization attributes. – pili Mar 07 '12 at 02:45
  • I want to change the "Stores" to some other name in the future. Is there a way that I can have it as attribute please? like DataContract – pili Mar 07 '12 at 02:50
  • Well, yes, you could create a class that has a property that holds the list, and decorate that property with the necessary attributes ([DataContract] on the class, then [DataMember(Name = "Whatever_you_want")] on the property itself. I just gave you an anonymous object example for the sake of brevity. – Chris Mar 07 '12 at 03:06
  • [DataMember(Name = "stores")] not working when I return it as asp.net mvc action result – pili Mar 07 '12 at 03:28
  • Did you make sure to add [DataContract] on the class definition? What version of MVC are you running? – Chris Mar 07 '12 at 05:01
  • 1
    Ahhh, by default MVC3 uses JavascriptSerializer. To use the DataContract / DataMember attributes, you'll need to force it to use the DataContractJsonSerializer as noted here: http://stackoverflow.com/questions/1302946/asp-net-mvc-controlling-serialization-of-property-names-with-jsonresult – Chris Mar 07 '12 at 05:12
  • Please note that such an implementation is only necessary if you really must support the attribute method. It would be easier up front if you could use the originally recommended anonymous object or just change the class's property name to match the desired output. – Chris Mar 07 '12 at 05:14
  • Thanks @Chris. I will try the DatacontractJson serializer – pili Mar 16 '12 at 20:11