Forum Moderators: open

Message Too Old, No Replies

error in parsing JSON text

         

kadnan

12:12 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



Hi

I am producing following JSON data via php:


{
"data": [
{
"student_id" :"5",
"first_name" :"X,
"last_name" :"XX",
"father_name" :"Y",
"dept_name" :"BS CS",
"roll_number" :"112"

},
{
"student_id" :"4",
"first_name" :"A",
"last_name" :"XX",
"father_name" :"DDD",
"dept_name" :"BS CS",
"roll_number" :"113"

}
]
};

when i put this data in a javascript variable then i am able to parse it by calling


var myI=objRec.data[0].first_name

but when i use xmlhttp object and recieve data via response text then i get err "data has no properties"

someone help me please to parse return data.

jshanman

12:40 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



When you receive responseText, it's a string. You need Javascript to evaluate it...

var myI=eval(xmlhttp.responseText);

It is one of the few cases where the use if eval is required.

- JS

kadnan

12:54 pm on Jun 20, 2006 (gmt 0)

10+ Year Member


but I am getting following javscript error in firefox:

invalid label
jsontest2.html (line 39)
"data": [

jshanman

1:17 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



Take out all the double quotes around the property names...

student_id :"5",
first_name :"X,
...

Instead of

"student_id" :"5",
"first_name" :"X,
...

Firefox will tolerate an extra comma after the last property in a block, but IE will die.

- JS

Bernard Marx

1:19 pm on Jun 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a missing double-quote:

"first_name" :"X",