I have a MVC action method from which I am returning the data as in the image below.
Action Method
[HttpPost]
public JsonResult LegalCheck(string jsonPackage)
{
var serializer = new JavaScriptSerializer();
var comments = serializer.Deserialize<Dictionary<string, string>>(jsonPackage.Substring(jsonPackage.IndexOf('{'), jsonPackage.LastIndexOf('}')));
var results = MyService.GetViolations(comments, CookieManager.ClientId);
return Json(results);
}

results holds the text area Id’s(CommentTextarea-1181_1183, etc) and needed data under that. How can i loop thorough the results? results.length, results[0] is showing as undefined. Is it possible to loop through this Json data or will have to change the json data that is being returned?
The data int he action method is as below
results
Count = 2
[0]: {[CommentTextarea_1181_1183, Mynamespace.Services.Legal[]]}
[1]: {[CommentTextarea_1181_1184, Mynamespace.Services.Legal[]]}
]}
Legal ia class as below
[Serializable]
public class Legal
{
public string Phrase { get; set; }
public int StartIndex { get; set; }
}
Thanks
It seems that the
MyService.GetViolationsmethod returns aDictionary<string, Legal[]>. TheJavaScriptSerializerthat is used by ASP.NET MVC 3 to serialize your JSON response doesn’t generate a javascript array when serializing a dictionary. It generates a javascript object. So you cannot loop because the indexes in this object are not 0 based integers.So in order to loop you could enumerate all the properties that are defined for the object: