Connecting Tech Pros Worldwide Forums | Help | Site Map

reading json object with jquery

Jon Paal [MSMD]
Guest
 
Posts: n/a
#1: Jun 27 '08
using json like

( {"Records": [ {"firstname":"Nancy","lastname":"Davolio"} ], "RecordCount":"1" } )

and jquery like:

$.ajax({
....

success: function(json, status) {
if(json.Records){alert("firstname= "+json.Records.firstname );}
....

I can retrieve values for firstname if I use the reference spelled out

is there a way to get it by numerical position, something like:

if(json.Records){alert("firstname= "+json.Records.0 );}

except it would work .. :)

thanks in advance



Jorge
Guest
 
Posts: n/a
#2: Jun 27 '08

re: reading json object with jquery


On May 4, 8:50*pm, "Jon Paal [MSMD]" <Jon nospam Paal @ everywhere dot
comwrote:
Quote:
using json like
>
( {"Records": [ {"firstname":"Nancy","lastname":"Davolio"} ], "RecordCount":"1" } )
>
and jquery like:
>
*$.ajax({
...
>
* * success: function(json, status) {
* * *if(json.Records){alert("firstname= "+json.Records.firstname );}
...
>
I can retrieve values for firstname if I use the reference spelled out
>
is there a way to get it by numerical position, something like:
>
* if(json.Records){alert("firstname= "+json.Records.0 );}
>
except it would work .. :)
>
thanks in advance
<html>
<head></head>
<body>
<script>
var jsonText, records, recordCount, i, a, b;

/*
If the data was serialized this way :
*/

jsonText='[["Nancy","Davolio"],["Jon","Paal"]]';

records = eval(jsonText);

/*
Then you could do :
*/
recordCount = records.length;
for (i=0; i<recordCount; i++) {
a = records[i][0];
b = records[i][1]
alert( b+", "+a);
}

/*
HTH,
--Jorge.
*/
</script>
</body>
</html>
Jon Paal [MSMD]
Guest
 
Posts: n/a
#3: Jun 27 '08

re: reading json object with jquery


thanks,

this will work, and if I still need formal json I'll go back to hard coding names....




Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#4: Jun 27 '08

re: reading json object with jquery


"Jon Paal [MSMD]" <Jon nospam Paal @ everywhere dot comwrites:
Quote:
using json like
>
( {"Records": [ {"firstname":"Nancy","lastname":"Davolio"} ], "RecordCount":"1" } )
>
and jquery like:
>
$.ajax({
...
>
success: function(json, status) {
if(json.Records){alert("firstname= "+json.Records.firstname );}
...
I hope it's "json.Records[0].firstname", since Records is an array.
Quote:
I can retrieve values for firstname if I use the reference spelled out
>
is there a way to get it by numerical position, something like:
>
if(json.Records){alert("firstname= "+json.Records.0 );}
json.Records[0] gives the first record.
To retrieve the "firstname" property, you need to use the "firstname"
property name. There is no ordering of named properties, so they don't
have a numerical index at all.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jorge
Guest
 
Posts: n/a
#5: Jun 27 '08

re: reading json object with jquery


On May 5, 5:57 am, "Jon Paal [MSMD]" <Jon nospam Paal @ everywhere dot
comwrote:
Quote:
thanks,
>
this will work, and if I still need formal json I'll go back to hard coding names....
Cool.

It's not stated clearly enough at json.org :
http://tools.ietf.org/html/rfc4627 : line 75 :
"A JSON text is a serialized object *** or array. ***"

But what parses as valid json ?


<html>
<head>
<style>
bad {
color: red;
}
blue {
color: blue;
}
</style>
<script src="http://www.JSON.org/json2.js">
/*
Please do NOT link to json.org,
use your own copy instead.
*/
</script>

</head>
<body>
<!--56789012345678901234567890123456789012345678901234 5678901234567890
-->
<script>
(function () {
var testIt = function (p) {
var i, name = prefix = msg = "";
var datesAsObjects = function (key, value) {
/*
see the reviver function in the source code :
json.org/json2.js line ~104..
this is an example function that intercepts Date(mm/dd/yyyy)
and turns it into a date object instead of a string.
It's NOT part of the json standard, it's just a quick
example of a reviver function. (and it has a bug).
*/
var d;
if (typeof value === 'string' &&
value.slice(0, 5) === 'Date(' &&
value.slice(-1) === ')') {
d = new Date(value.slice(5, -1));
if (d) {
return d;
}
}
return value;
}
try {
data = JSON.parse(p, datesAsObjects);
/*
See the source : json.org/json2.js
*/
if (typeof data === 'object') {
if (data.constructor === Array) {
msg = "an object : Array : ";
for (i=0;i<data.length;i++) {
msg += prefix+"e["+i+"]: "+typeof data[i]+" = "+data[i];
prefix = ", ";
}
} else if (data.constructor === Object) {
msg = "an object : Object : ";
for (name in data) {
if (data.hasOwnProperty(name)) {
msg += prefix+name+": "+typeof data[name]+" = "+data[name];
}
}
} else {
msg="an object whose constructor is : "+data.constructor;
}
} else {
msg = "a " + typeof data + ": " + data;
}
document.write("The string "+p+" was parsed as");
document.write(" VALID json.\nIt produced " + msg + "<br>");
} catch (e) {
document.write("<bad>The string "+p+" is NOT");
document.write(" valid json.</bad><br>");
}
};

/*
In theory, just arrays and objects,
in practice :
let's see what's what (valid json?) :
*/

testIt('01/15/2008'); testIt('[01/15/2008]');
testIt('15/01/2008'); testIt('[15/01/2008]');
testIt('"01/15/2008"'); testIt('["01/15/2008"]');
testIt('"15/01/2008"'); testIt('["15/01/2008"]');
testIt('Date(15/01/2008)'); testIt('[Date(15/01/2008)]');
testIt('Date(01/15/2008)'); testIt('[Date(01/15/2008)]');
/*
the next one gives the date wrong.
*/
document.write("<blue>");
testIt('"Date(15/01/2008)"'); testIt('["Date(15/01/2008)"]');
document.write("</blue>");
testIt('"Date(01/15/2008)"'); testIt('["Date(01/15/2008)"]');
testIt('true'); testIt('[true]');
testIt('True'); testIt('[True]');
testIt('false'); testIt('[false]');
testIt('False'); testIt('[False]');
testIt('null'); testIt('[null]');
testIt('Null'); testIt('[Null]');
testIt('"true"'); testIt('["true"]');
testIt('"True"'); testIt('["True"]');
testIt('"false"'); testIt('["false"]');
testIt('"False"'); testIt('["False"]');
testIt('"null"'); testIt('["null"]');
testIt('"Null"'); testIt('["Null"]');
testIt('Hi there !'); testIt('[Hi there !]');
testIt('"Hi there !"'); testIt('["Hi there !"]');
testIt('99'); testIt('[99]');
testIt('99.98'); testIt('[99.98]');
testIt('9.98e-16'); testIt('[9.98e-16]');
testIt('"99"'); testIt('["99"]');
testIt('"99.98"'); testIt('["99.98"]');
testIt('"9.98e-16"'); testIt('["9.98e-16"]');
testIt('[["Nancy","Davolio"],["Jon","Paal"]]');
/*
the next one gives an invalid date.
*/
document.write("<blue>");
testIt('["Date(01/15/2008)","Date()","Date","99",99]');
document.write("</blue>");
testIt('{"aProperty":99}');
testIt('"function(){alert(window.location.href)}"' );
testIt('function(){alert(window.location.href)}');
testIt('"window.aGlobalVar=99"'); testIt('window.aGlobalVar=99');

/*
HTH
--Jorge.
*/
})();
</script>
</body>
</html>
jdalton
Guest
 
Posts: n/a
#6: Jun 27 '08

re: reading json object with jquery


The jQuery support list is here:
http://groups.google.com/group/jquery-en
Jorge
Guest
 
Posts: n/a
#7: Jun 27 '08

re: reading json object with jquery


On May 5, 3:07*pm, jdalton <John.David.Dal...@gmail.comwrote:
Quote:
The jQuery support list is here:http://groups.google.com/group/jquery-en
The json support list is here:
http://tech.groups.yahoo.com/group/json/
Jon Paal [MSMD]
Guest
 
Posts: n/a
#8: Jun 27 '08

re: reading json object with jquery


A solution to the original json structure:

success: function(json, status) {
var records = json.Records;
var str = "";
if(records ){
for(var i = 0; i < records.length; i++){
for(var j in records[i]){
str += j + " --" + records[i][j] + "\n";
}
}
alert(str);
}





"Jon Paal [MSMD]" <Jon nospam Paal @ everywhere dot comwrote in message news:McCdnX-gdIMbmIPVnZ2dnUVZ_s-pnZ2d@palinacquisition...
Quote:
using json like
>
( {"Records": [ {"firstname":"Nancy","lastname":"Davolio"} ], "RecordCount":"1" } )
>
and jquery like:
>
$.ajax({
...
>
success: function(json, status) {
if(json.Records){alert("firstname= "+json.Records.firstname );}
...
>
I can retrieve values for firstname if I use the reference spelled out
>
is there a way to get it by numerical position, something like:
>
if(json.Records){alert("firstname= "+json.Records.0 );}
>
except it would work .. :)
>
thanks in advance
>

Jorge
Guest
 
Posts: n/a
#9: Jun 27 '08

re: reading json object with jquery


On May 6, 5:27*am, "Jon Paal [MSMD]" <Jon nospam Paal @ everywhere dot
comwrote:
Quote:
A solution to the original json structure:
>
success: function(json, status) {
* var records = json.Records;
* var str = "";
* if(records ){
* *for(var i = 0; i < records.length; i++){
* * for(var j in records[i]){
* * *str += j + " --" + records[i][j] + "\n";
* * }
* *}
* alert(str);
* }
>
Yep !

Still, I'd consider that :

{"Records":
[{"firstname":"Nancy","lastname":"Davolio"}],"RecordCount":"1"}

contains as much info as :

[["Nancy","Davolio"]]

Bus is almost 3x longer.

If you want the headers, you can save them into Records[0] (and keep
sending them but only once) :

[["firstname","lastname"],["Nancy","Davolio"]]

Which still is more compact than the original structure :

[["firstname","lastname"],["Nancy","Davolio"]]
{"Records":
[{"firstname":"Nancy","lastname":"Davolio"}],"RecordCount":"1"}

and tends to be still more compact as the # of records go up : 175 vs
340 chars. (~50%)

[
["firstname","lastname"],
["Nancy","Davolio"],
["Nancy","Davolio"],
["Nancy","Davolio"],
["Nancy","Davolio"],
["Nancy","Davolio"],
["Nancy","Davolio"],
["Nancy","Davolio"]
]

==

{"Records":[
{"firstname":"Nancy","lastname":"Davolio"},
{"firstname":"Nancy","lastname":"Davolio"},
{"firstname":"Nancy","lastname":"Davolio"},
{"firstname":"Nancy","lastname":"Davolio"},
{"firstname":"Nancy","lastname":"Davolio"},
{"firstname":"Nancy","lastname":"Davolio"},
{"firstname":"Nancy","lastname":"Davolio"}],
"RecordCount":"3"}

And the code is almost the same :

success: function(json, status) {
var i, j, r = json, str = "";
if (r) {
for (i=1; i<r.length; i++) {
for (j=0; j<r[i].length; j++) {
str += r[0][j] + " --" + r[i][j] + "\n";
}
}
alert (str);
}

--Jorge.
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#10: Jun 27 '08

re: reading json object with jquery


Jorge wrote:
Quote:
On May 5, 3:07 pm, jdalton <John.David.Dal...@gmail.comwrote:
Quote:
>The jQuery support list is here:http://groups.google.com/group/jquery-en
>
The json support list is here:
http://tech.groups.yahoo.com/group/json/
Peer reviews are included here:
http://groups.google.com/groups?q=%2...ing=d&filter=0


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
Closed Thread


Similar JavaScript / Ajax / DHTML bytes