How do I include the array name in my Json response?
In my controller I have the following开发者_运维问答 code to generate a Json response whenever a particular URL is hit.
public JsonResult LatLng()
{
var zones = zoneRepository.GetCoordinates().ToList();
return Json(zones, JsonRequestBehavior.AllowGet);
}
The output looks like this.
[{"Zip_Code":1001,"City":"Agawam","DMA":"SPRINGFIELD, MA"},...
My question is the following. Shouldn't there be an array name preceding the Json array? For example:
{"zones":[{"Zip_Code":...
When I parse this using Jquery I need to use something along the lines of $.each(json.zones so I'm wondering how to include the array name in the Jason response. Thanks!
try
return Json(new { zones = zones }, JsonRequestBehavior.AllowGet);
This example from json.org may help clarify...
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
Using this example as a starting point, I would say you would do this (imagining the zones were different!):
{"zones": [
{"Zip-Code": "1001", "City": "Agawam", "DMA": "SPRINGFIELD, MA"},
{"Zip-Code": "1001", "City": "Agawam", "DMA": "SPRINGFIELD, MA"},
{"Zip-Code": "1001", "City": "Agawam", "DMA": "SPRINGFIELD, MA"}
]
}
JSONObject title = new JSONObject();
JSONObject main = new JSONObject();
title.put("zone", main);
main.put("Zip Code", value);
main.put("City", value);
........
精彩评论