0

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?

When i print it to the console, it looks like this: enter image description here

4
  • 1
    Have you tried to print the data without JSON.parse? For example just console.log(returnedData); ? Commented Jun 9, 2020 at 11:27
  • 1
    Note that queryable.Where(lambda).Count() == 0 will almost always perform faster when written as !queryable.Any(lambda). Commented Jun 9, 2020 at 13:11
  • Does this answer your question? JSON parsing error - Unexpected token o in JSON at position 1 Commented Jun 9, 2020 at 13:17
  • The reason for the error was that you were running JSON.parse on returnedData, which was not JSON, but an object that had already been parsed from the JSON. Commented Jun 9, 2020 at 13:23

1 Answer 1

0

I have figured out the following workaround. I changed my data type from Dictionary to a list of anonymous types:

public JsonResult GenerateLicensePlateSeries(string letters, int number, int amount)
        {
            List<Object> plates = new List<Object>();            

            var table = luxWebSamContext.LicensePlates;            
            do
            {
                if (table.Where(x => x.LicensePlateNumber == (letters + number)).Count() == 0)
                {                    
                    var v = new 
                    { 
                        plate = letters + (number), 
                        assignedTo = "" 
                    };

                    plates.Add(v);
                }
                else
                {                 
                    var v = new 
                    { 
                        plate = letters + (number), 
                        assignedTo = table.Where(x => x.LicensePlateNumber == (letters + number)).Single().CommissionNumber
                    };
                    plates.Add(v);
            }

                number++;

            }
            while (plates.Count < amount);

            return Json(plates);
        }

Now my response looks like that:

enter image description here

And i can access it: in the ajax method:

success: function (returnedData) {                       



                for (var i = 0; i < returnedData.length; i++) {
                    alert(returnedData[i].plate);
                    alert(returnedData[i].assignedTo);
                }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.