alert( json.title ); says undefined

the header is application/json

have tried various bits and bobs but still not getting results

any suggestions

On 9/18/06, Klaus Hartl <[EMAIL PROTECTED]> wrote:
Sam Sherlock schrieb:
> Ealier Klaus recommended usig JSON to process an object
>
> here is the code I am using
>
> $.ajax({
>     url: "./content/?" + htmlDoc,
>     dataType: 'json',
>     success: function(json) {
>         // process json...
>
>                         eval("var myVar = " + json +";"); // this line
> is causing an error. (see below)
>
>
>                         console.info('    title0: ' + myVar.item[0].title);
>
>
>     },
>     error: function() {
>     }
> })
>
> my object looks like this:
> {'title':'credits', 'content':'    <div id="credits"><h2>Credits</h2>
> <div><p>Content</p></div></div>', 'image':'<img
> src="" width="467" height="750" alt="" />',
> 'music':8}
>
> also should I have quotes around the prop name aswell as the value of
> the prop? some example do and some don't maybe this is optional?
>
> the error caused by the eval is
>                     missing ] after element list http://ss29/... (line 104)
>                     var myVar = [object XMLHttpRequest];
>
> what I am doing wrong?
>


Sam, you don't need no eval. This is all already handled by jQuery
itself, since you have defined 'json' for dataType.

You can simply access the properties of the returned (json) object literal:

success: function(json) {
     alert( json.title );
}


As per JSON definition[1], the property names must be strings, you
therefore should leave them wrapped in quotes. I also read somewhere
that otherwise you can run into some strange problems (cannot find it
anywhere right now).

The property values can be one of the _javascript_ data types

string
number
object
array
boolean
null

i.e. only use quotes if you have a string.


HTH, Klaus


[1]http://json.org/

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to