New to JSON

Discussion in 'Javascript' started by Tom Cole, Aug 16, 2006.

  1. Tom Cole

    Tom Cole Guest

    I'm new to JSON but see how it can be an improvement over XML for some
    things.

    I've modified a test servlet to return the following string with a
    content type of text/x-json:

    { header: { date: "16 Aug 2006 19:53:03 GMT", headers: [ { name:
    "accept", value: "*/*" }, { name: "accept-language", value: "en-us" },
    { name: "referer", value: "http://njep11/Ajax/" }, { name:
    "content-type", value: "application/x-www-form-urlencoded" }, { name:
    "accept-encoding", value: "gzip, deflate" }, { name: "user-agent",
    value: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR
    1.0.3705; .NET CLR 1.1.4322)" }, { name: "host", value: "njep11" }, {
    name: "content-length", value: "9" }, { name: "connection", value:
    "Keep-Alive" }, { name: "cache-control", value: "no-cache" }] } }

    On the client side I receive this an create an object via eval:

    var object = eval('(' + xmlhttp.respongeText + ')');

    If I do an alert(object.toString()) I get [Object object]. So I thought
    it was working okay. However if I try to access object.headers[0].name
    I get an error stating "headers.0 is null or not an object.

    Is my JSON string correct? I'm not sure what role (if any) whitespace
    plays in this type. I'm expecting it to create a object of type header
    with a variable date and an array of objects that contain name and
    value variables.

    Thanks in advance.
    Tom Cole, Aug 16, 2006
    #1
    1. Advertising

  2. Tom Cole

    Tom Cole Guest

    Sorry for the typo, it is responseText. And I've validated that the
    text was sent properly as my JSON paste in this post was a cut and
    paste of the response.

    Thanks.

    Tom Cole wrote:
    > I'm new to JSON but see how it can be an improvement over XML for some
    > things.
    >
    > I've modified a test servlet to return the following string with a
    > content type of text/x-json:
    >
    > { header: { date: "16 Aug 2006 19:53:03 GMT", headers: [ { name:
    > "accept", value: "*/*" }, { name: "accept-language", value: "en-us" },
    > { name: "referer", value: "http://njep11/Ajax/" }, { name:
    > "content-type", value: "application/x-www-form-urlencoded" }, { name:
    > "accept-encoding", value: "gzip, deflate" }, { name: "user-agent",
    > value: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR
    > 1.0.3705; .NET CLR 1.1.4322)" }, { name: "host", value: "njep11" }, {
    > name: "content-length", value: "9" }, { name: "connection", value:
    > "Keep-Alive" }, { name: "cache-control", value: "no-cache" }] } }
    >
    > On the client side I receive this an create an object via eval:
    >
    > var object = eval('(' + xmlhttp.respongeText + ')');
    >
    > If I do an alert(object.toString()) I get [Object object]. So I thought
    > it was working okay. However if I try to access object.headers[0].name
    > I get an error stating "headers.0 is null or not an object.
    >
    > Is my JSON string correct? I'm not sure what role (if any) whitespace
    > plays in this type. I'm expecting it to create a object of type header
    > with a variable date and an array of objects that contain name and
    > value variables.
    >
    > Thanks in advance.
    Tom Cole, Aug 16, 2006
    #2
    1. Advertising

  3. Tom Cole

    Tom Cole Guest

    Got it:

    object.header.headers[0].name worked. I was assuming that the object
    created was a header, not that it contained a header.

    Thanks.
    Tom Cole, Aug 16, 2006
    #3
  4. Tom Cole

    Guest

    Tom Cole wrote:
    > I'm new to JSON but see how it can be an improvement over XML for some
    > things.
    >
    > I've modified a test servlet to return the following string with a
    > content type of text/x-json:
    >
    > { header: { date: "16 Aug 2006 19:53:03 GMT", headers: [ { name:
    > "accept", value: "*/*" }, { name: "accept-language", value: "en-us" },
    > { name: "referer", value: "http://njep11/Ajax/" }, { name:
    > "content-type", value: "application/x-www-form-urlencoded" }, { name:
    > "accept-encoding", value: "gzip, deflate" }, { name: "user-agent",
    > value: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR
    > 1.0.3705; .NET CLR 1.1.4322)" }, { name: "host", value: "njep11" }, {
    > name: "content-length", value: "9" }, { name: "connection", value:
    > "Keep-Alive" }, { name: "cache-control", value: "no-cache" }] } }
    >
    > On the client side I receive this an create an object via eval:
    >
    > var object = eval('(' + xmlhttp.respongeText + ')');
    >
    > If I do an alert(object.toString()) I get [Object object]. So I thought
    > it was working okay. However if I try to access object.headers[0].name
    > I get an error stating "headers.0 is null or not an object.
    >
    > Is my JSON string correct? I'm not sure what role (if any) whitespace
    > plays in this type. I'm expecting it to create a object of type header
    > with a variable date and an array of objects that contain name and
    > value variables.
    >
    > Thanks in advance.


    Hi Tom,

    With JSON the keys in the key value pairs also need to be in quotes.
    Also you need to access the object from header. Below is a working
    example of your code:

    <script type="text/javascript">

    var text = "{ 'header': { 'date': '16 Aug 2006 19:53:03 GMT',
    'headers': [ { 'name': 'accept', 'value': '*/*' }, { 'name':
    'accept-language', 'value': 'en-us' }, { 'name': 'referer', 'value':
    'http://njep11/Ajax/' }, { 'name':'content-type', 'value':
    'application/x-www-form-urlencoded' }, { 'name':'accept-encoding',
    'value': 'gzip, deflate' }, { 'name': 'user-agent', 'value':
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705;
    ..NET CLR 1.1.4322)' }, { 'name': 'host', 'value': 'njep11' }, { 'name':
    'content-length', 'value': '9' }, { 'name': 'connection', 'value':
    'Keep-Alive' }, { 'name': 'cache-control', 'value': 'no-cache' }] } }";

    //On the client side I receive this an create an object via eval:

    var object = eval('(' + text + ')');

    alert("object=" + object.header.headers[0].name);
    </script>

    Paste this into any page to test.

    -Greg
    , Aug 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    229
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    316
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    257
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    155
    David Karr
    Jun 17, 2013
Loading...

Share This Page