I have a controller method that returns a Dictionary to a JQuery ajax function:
public JsonResult GenerateLicensePlateSeries(string letters, int number, int amount)
{
Dictionary<string, string> plates = new Dictionary<string, string>();
var table = luxWebSamContext.LicensePlates;
do
{
if (table.Where(x => x.LicensePlateNumber == (letters + number)).Count() == 0)
{
plates.Add(letters + (number), "");
}
else
{
plates.Add(letters + (number), table.Where(x => x.LicensePlateNumber == (letters + number)).Single().CommissionNumber);
}
number++;
}
while (plates.Count < amount);
return Json(plates);
}
That code returns the following string:
{"AB2495":"","AB2496":"","AB2497":"","AB2498":"","AB2499":"","AB2500":"095753125"}
My problem is that i don't know how to access this data in the success function of my post method. I would like to iterate over the response and check if the second field of each key/pair value is populated with a number (the last one is):
$.post(baseUrl + '/de/SAM/GenerateLicensePlateSeries',
{
letters: $("#seriesLetters").val(),
number: $("#seriesNumbers").val(),
amount: $("#seriesAmount").val()
},
function (returnedData) {
var data = JSON.parse(returnedData);
alert(data);
});
I tried to parse it to JSON but i received an error:
Uncaught SyntaxError: Unexpected token o in JSON at position 1
Any ideas how to access the data?