vote up 0 vote down
star

I get a parse error when using jQuery to load some JSON data. Here's a snippet of my code:

jQuery.ajax({
    dataType: "json",

    success: function (json)
    {
        jQuery.each(json, function ()
        {
            alert(this["columnName"]);
        });
    }
});

I get no errors when parsing a non-empty JSON object. So my guess is that the problem is with my serializer.


Question is: how do I format an empty JSON object which jQuery won't consider malformed?

This is what I've tried so far, with no success:

{[]}

{[null]}

{}

{null}


{"rows": []}

{"rows": null}

{"rows": {}}



UPDATE:

I can understand that I've been somewhat vague--let me try and clarify:

Parsing of the JSON object is not the issue here--JQuery is - I think.

jQuery throws a parse-error (invokes the error function). It seems like jQuery's internal JSON validation is not accepting any of the before mentioned objects. Not even the valid ones.

Output of the error function is:

XMLHttpRequest: XMLHttpRequest readyState=4 status=200
textStatus: parsererror
errorThrown: undefined

This goes for all of the before mentioned objects.

offensive?
comments (4)

4 Answers:

vote up 1 vote down

Firstly {[]}, {[null]}, and {null} won't work because they are all invalid JSON objects (as verified by the JSON validator).

The remaining objects are all valid JSON objects, so your success function should be getting invoked.

If you pass a non-array or array-like object object then the each function will enumerate your json object by its named properties. In the case of your three objects that each have a rows property this will be set to [], null, and {} respectively, none of which have a columnName attribute so an undefined error will be thrown.

Your {} object on the other hand has no properties, so shouldn't be causing an error because the each call will loop 0 times. What does the following line display if you add it as the first line in your success function?

alert(typeof json + ' ' + (json == null));
link|offensive?
add comment
vote up 0 vote down

Instead of:

$(json).each(function () { ... });

I think you want to be using:

$.each(json, function () { ... });

From the jQuery.each documentation:

This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.

link|offensive?
comments (1)
vote up 0 vote down

Did you verify if the JSON is returned correctly in the first place before the each? Set the empty value as {}, and verify if it is like that before .each

Also it would help to know how your JSON looks like when there is data.

link|offensive?
add comment
vote up 0 vote down

you are looking for this:

[]

an empty array. That is, if you plan to iterate over it right away, then what you need is an array.

link|offensive?
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.