0

I am trying to use jQuery to iterate through the objects in this JSON expression:

[
    {
        "value": 609,
        "label": "Wyandotte, MI"
    },
    {
        "value": 3141,
        "label": "Wilmington, NC"
    }
]

Such that each object consists of two properties, value and label.

This is what I have so far:

$(data).each(function () {
     alert('value: ' + this.value + ' label: ' + this.label);
});

But I keep getting an error: Uncaught Error: Syntax error, unrecognized expression

Can anyone help?

Edit:
The data variable in the example was arbitrary. What is actually happening is I'm getting an array of objects something like this:

data =
[
    locations: "[{"value":5626,"label":"Bensenville, IL"}]",
    notes: "Sample note"
]

Then I call data = JSON.parse(data);

And then I perform the iteration on data.locations, which is producing the error.

Edit (again):
https://jsfiddle.net/e2p7gdod/
I keep trying to reproduce what I'm seeing.
This is how I'm producing my return result:

public JsonResult Foo()
{
    var data = new JObject();
    data["locations"] = "[{'value': 609,'label': 'Wyandotte, MI'},{'value': 3141,'label': 'Wilmington, NC'}]";
    data["supervisor"] = "John Doe";
    data["notes"] = "Sample note";

    return new JsonResult()
    {
        Data = JsonConvert.SerializeObject(data),
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}
8
  • 2
    Your code as posted does not produce the error. Exactly what happens before your .each() call? Where does data get its value? Commented Jul 10, 2015 at 20:53
  • Works fine as-is: jsfiddle.net/yk4wb7dv (assuming your array is data) Commented Jul 10, 2015 at 20:56
  • 1
    The data in your Edit is invalid syntax. When you have keys and values, they have to be in an object, not an array. It should be data = { locations: ..., notes: "Sample note" } Commented Jul 10, 2015 at 21:20
  • 1
    And if you're going to pass it to JSON.parse, the value of data should be a string, not an array. Commented Jul 10, 2015 at 21:21
  • The JSON is invalid. Strings in JSON have to be in double quotes, not single quotes. Commented Jul 10, 2015 at 21:50

2 Answers 2

1

It looks like you're trying to put a JSON object in data.locations, but you have the syntax wrong. Strings in JSON must be enclosed in double quotes, not single quotes.

public JsonResult Foo()
{
    var data = new JObject();
    data["locations"] = "[{\"value\": 609,\"label\": \"Wyandotte, MI\"},{\"value\": 3141,\"label\": \"Wilmington, NC\"}]";
    data["supervisor"] = "John Doe";
    data["notes"] = "Sample note";

    return new JsonResult()
    {
        Data = JsonConvert.SerializeObject(data),
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

Then in the Javascript, when you want to iterate over it, you need to call JSON.parse(data.locations):

var locations = JSON.parse(data.locations);
$.each(locations, function() {
    alert('value: ' + this.value + ' label: ' + this.label);
});
Sign up to request clarification or add additional context in comments.

2 Comments

The second part was the problem. I was calling JSON.parse on the data and then calling $(data.divisions).each(...) but I was not calling $(JSON.parse(data.divisions)).each()!!! Thank you thank you thank you! :-)
Why are you putting JSON into data["locations"] in the first place? Why not just put a C# array of objects there?
1

That should be the static version of each, passing the array (faster than wrapping the array in a jQuery object, just to iterate over it):

$.each(data, function () {
     alert('value: ' + this.value + ' label: ' + this.label);
});

Note: Your current code does not give that error and appears to work fine:

http://jsfiddle.net/yk4wb7dv/

Have you included JQuery etc?

6 Comments

Probably better, but the code as posted won't cause the error reported.
@Pointy: $(data) probably would cause that error if data is his object as I suspect.
Well the OP says that data is an array. If that's the case, then jQuery will just wrap itself around the array.
I've always done function(index, item) { ... } and then referenced item in the block. "this" handling in JS scares me. :)
@Fiid: You can do that too. No need to be afraid of this though :)
|

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.