473,378 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

New to JSON

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.

Aug 16 '06 #1
3 1667
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.
Aug 16 '06 #2
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.

Aug 16 '06 #3

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 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
8
by: Douglas Crockford | last post by:
There is a new version of JSON.parse in JavaScript. It is vastly faster and smaller than the previous version. It uses a single call to eval to do the conversion, guarded by a single regexp test to...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
3
by: Adam | last post by:
I'm trying to retrieve some values from a json object, but instead it's giving me the property name. For example: var json = { "glossary": { "title": "example glossary" } }; console.log(json);...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.