I have tried various versions compressed and noncompressed

the current jquery version info is:
  $Date: 2006-08-31 13:26:31 -0400 (Thu, 31 Aug 2006) $
  $Rev: 249 $



On 9/18/06, Sam Sherlock <[EMAIL PROTECTED]> wrote:
I have just had partial success with the following:


                    success: function(json) {

                        console.info( json );

                        obj = eval('('+json.responseText+')');
                       
                        console.info('    title: ' + obj.title);
                        console.info('    content: ' + obj.content);
                        console.info('    image: ' + obj.image);
                        console.info('    music: ' + obj.music);

                        isInit = false;                       
                        init();
                    }

which I deduced from the information released by console.info(json)

perhaps there is a draw back to this approach?

I am interested to hear thoughts opinions etc

thanks especially to Francisco Brito and Klaus Hartl

On 9/18/06, Sam Sherlock < [EMAIL PROTECTED]> wrote:
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
discuss@jquery.com
http://jquery.com/discuss/



_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to