Problem with JSON implementation

P

pranny

Hi All,

I am trying to use AJAX in ColdFusion. It is very simple. I have a
'requesting page' and a 'responsive page'. The 'responsive page' sends
the data in JSON format. However, alongwith my JSON data are various
other technical debugging info attached. So i use a REGEX on the
response to find out my JSON data.

My JSON data looks something like

{"Unassigned_Instructors":[{"name":"Todd Anderson","id":133693},
{"name":"Instructor Default","id":4},{"name":"Prak Pran","id":133701},
{"name":"prakash Pranav","id":133699},{"name":"s m","id":
133700}],"Unassigned_Equipment":[],"Classrooms":
[],"Assigned_Equipment":[],"Assigned_Instructors":[]};

I am storing it in a vairbale myJSONOb

The issue is that after using REGEX, it seems to be that the JSOn is
converted to String object, so it is unusable.
alert(typeof(myJSONOb)) shows Object
alert(myJSONOb.Unassigned_Instructors[0].name) gives an undefined
error.

Where i might be going wrong. I am a newbie, not much into AJAX and
Javascript. Also please dont ask me to use ColdFusion AJAX libraries.
I need to implement this AJAX without any library.

KAny help would be great.

Thanks !!
 
P

pranny

With some experiments, i managed to get something

if(xmlHttp.readyState==4){
var raw_result = xmlHttp.responseText;
refined_result = raw_result.match('{\.*}');
var myJSONObject = refined_result;
var ss = document.createElement('script');
var scr = 'var myB = '+ myJSONObject + ';';
scr = scr + 'alert(myB.Unassigned_Instructors.instructor[0].name);';
var tt = document.createTextNode(scr);
ss.appendChild(tt);
var hh = document.getElementsByTagName('head')[0];
hh.appendChild(ss);
alert(myB.Unassigned_Instructors[0].name);
}

This works great :) atleast i see an alert of Todd Anderson in this
case. But if there is a better way to do, i will be more happy.

Thanks
 
P

pranny

With some experiments, i managed to get something

if(xmlHttp.readyState==4){
 var raw_result = xmlHttp.responseText;
 refined_result = raw_result.match('{\.*}');
 var myJSONObject = refined_result;
 var ss = document.createElement('script');
 var scr = 'var myB = '+ myJSONObject + ';';
 scr = scr + 'alert(myB.Unassigned_Instructors.instructor[0].name);';
 var tt = document.createTextNode(scr);
 ss.appendChild(tt);
 var hh = document.getElementsByTagName('head')[0];
 hh.appendChild(ss);
 alert(myB.Unassigned_Instructors[0].name);

}

This works great :) atleast i see an alert of Todd Anderson in this
case. But if there is a better way to do, i will be more happy.

Thanks

I am trying to use AJAX in ColdFusion. It is very simple. I have a
'requesting page' and a 'responsive page'. The 'responsive page' sends
the data in JSON format. However, alongwith my JSON data are various
other technical debugging info attached. So i use a REGEX on the
response to find out my JSON data.
My JSON data looks something like
{"Unassigned_Instructors":[{"name":"Todd Anderson","id":133693},
{"name":"Instructor Default","id":4},{"name":"Prak Pran","id":133701},
{"name":"prakash Pranav","id":133699},{"name":"s m","id":
133700}],"Unassigned_Equipment":[],"Classrooms":
[],"Assigned_Equipment":[],"Assigned_Instructors":[]};
I am storing it in a vairbale myJSONOb
The issue is that after using REGEX, it seems to be that the JSOn is
converted to String object, so it is unusable.
alert(typeof(myJSONOb)) shows Object
alert(myJSONOb.Unassigned_Instructors[0].name) gives an undefined
error.
Where i might be going wrong. I am a newbie, not much into AJAX and
Javascript. Also please dont ask me to use ColdFusion AJAX libraries.
I need to implement this AJAX without any library.
KAny help would be great.
Thanks !!

The above method does not works with IE :-(. So, i have to look for
something better
 
T

Thomas 'PointedEars' Lahn

pranny said:
I am trying to use AJAX in ColdFusion. It is very simple. I have a
'requesting page' and a 'responsive page'. The 'responsive page' sends
the data in JSON format. However, alongwith my JSON data are various
other technical debugging info attached. So i use a REGEX on the
response to find out my JSON data.

That will only work if the "other technical debugging info" does not contain
either `{' or `}'.
My JSON data looks something like

{"Unassigned_Instructors":[{"name":"Todd Anderson","id":133693},
{"name":"Instructor Default","id":4},{"name":"Prak Pran","id":133701},
{"name":"prakash Pranav","id":133699},{"name":"s m","id":
133700}],"Unassigned_Equipment":[],"Classrooms":
[],"Assigned_Equipment":[],"Assigned_Instructors":[]};

I am storing it in a vairbale myJSONOb
How?

The issue is that after using REGEX, it seems to be that the JSOn is
converted to String object, so it is unusable.

The conversion is internally only.
alert(typeof(myJSONOb)) shows Object

That's possible, but unlikely. It is more likely that it shows `object', in
which case the value of `myJSONOb' may be either an object reference or `null'.

BTW: Should be window.alert(), and `typeof' is an operator (you should lose
the parens.)
alert(myJSONOb.Unassigned_Instructors[0].name) gives an undefined ^^^^^^^^^
error.
^^^^^^
There is no such thing.
Where i might be going wrong.

Impossible to say, you don't even tell the exact error message, let alone
the relevant source code.
[...]
I need to implement this AJAX without any library.

Good idea.

Please take heed of <http://jibbering.com/faq/#posting>.


PointedEars
 
T

Thomas 'PointedEars' Lahn

String.prototype.match() expects a reference to a RegExp object as argument.
If that is not the case, as here, the argument (e.g. `regexp') is replaced
with the result of `new RegExp(regexp)'.

The `\' is pointless here, but not harmful: `\.' is not a registered string
escape sequence, so it will be regarded a single dot before passed to match().

The above method does not works with IE :-(.

"Does not work" is a useless error description. [psf 4.11]

<http://jibbering.com/faq/#FAQ4_43>

Probably it does not work with IE because JScript works differently than
JavaScript (or whatever ECMAScript implementation you have been using, you
didn't tell about it or the browser).

new RegExp('{.*}')

may be considered invalid, because `{' and `}' are reserved characters in
Regular Expressions (for repetition).
So, i have to look for something better

Possibilities:

A. (recommended)

var refined_result = raw_result.match(/\{.*\}/);

B.

var refined_result = raw_result.match("\\{.*\\}");

But as I said before, that still does not need to work. Certain conditions
must apply before you can use Regular Expressions to parse matching tokens
(like braces). See the Chomsky hierarchy for details.

Read, write, test, post; in *that* order.


PointedEars
 
P

pranny

String.prototype.match() expects a reference to a RegExp object as argument.
 If that is not the case, as here, the argument (e.g. `regexp') is replaced
with the result of `new RegExp(regexp)'.

The `\' is pointless here, but not harmful: `\.' is not a registered string
escape sequence, so it will be regarded a single dot before passed to match().
The above method does not works with IE :-(.

"Does not work" is a useless error description. [psf 4.11]

<http://jibbering.com/faq/#FAQ4_43>

Probably it does not work with IE because JScript works differently than
JavaScript (or whatever ECMAScript implementation you have been using, you
didn't tell about it or the browser).

  new RegExp('{.*}')

may be considered invalid, because `{' and `}' are reserved characters in
Regular Expressions (for repetition).
So, i have to look for something better

Possibilities:

A. (recommended)

  var refined_result = raw_result.match(/\{.*\}/);

B.

  var refined_result = raw_result.match("\\{.*\\}");

But as I said before, that still does not need to work.  Certain conditions
must apply before you can use Regular Expressions to parse matching tokens
(like braces).  See the Chomsky hierarchy for details.

Read, write, test, post; in *that* order.

PointedEars

Thanks PointedEars,
I am a little frustrated because of this thing. All i want is my JSON
object (post #1) send by response page to be used by the caller page.


I can understand from you post that using RegExp does not make any
difference (if the regex is correct). So
var myJSONObject = refined_result;
is perfectly legal and will generate a Javascript object and i can use
it.

In IE, it gives an error in following line
ss.appendChild(tt);

saying that this is unexpected behavior.
 
T

Thomas 'PointedEars' Lahn

pranny said:
Thomas said:
pranny said:
pranny wrote:
With some experiments, i managed to get something
if(xmlHttp.readyState==4){
var raw_result = xmlHttp.responseText;
refined_result = raw_result.match('{\.*}');
String.prototype.match() expects a reference to a RegExp object as argument.
If that is not the case, as here, the argument (e.g. `regexp') is replaced
with the result of `new RegExp(regexp)'.
[...]
Read, write, test, post; in *that* order.
[...]

Please trim your quotes to the relevant minimum (final request).
Thanks PointedEars,
I am a little frustrated because of this thing.

Following my advice instead of making the same mistake as before would have
eased your tensions already. Much the same as one does not get to drive a
car (legally) without a driver's license, it is hardly possible to make use
of a programming language before having studied and understood its rules and
features.
All i want is my JSON object (post #1) send by response page to be used
by the caller page.

Since with `responseText' a HTTP response message is necessarily represented
as a primitive string value, you need to convert (the relevant parts of) it
to an object. There are several techniques available for this; the most
simple is passing the string to eval() and using the return value of the
call. RTFFAQ, STFW.
I can understand from you post that using RegExp does not make any
difference (if the regex is correct).

It is supposed to make a difference in performance, of course, because no
internal conversion is necessary then.
So
var myJSONObject = refined_result;
is perfectly legal

"Legal" as in "syntactically correct"; not as in "useful" or "necessary".
and will generate a Javascript object and i can use it.

No, it will assign a reference to an object that encapsulates the strings
that the argument would match if converted (as described) to a RegExp object.

RTFM:
In IE, it gives an error in following line
ss.appendChild(tt);

saying that this is unexpected behavior.

No, it doesn't.


PointedEars
 
J

Jorge

With some experiments, i managed to get something

if(xmlHttp.readyState==4){
 var raw_result = xmlHttp.responseText;
 refined_result = raw_result.match('{\.*}');
 var myJSONObject = refined_result;
 var ss = document.createElement('script');
 var scr = 'var myB = '+ myJSONObject + ';';
 scr = scr + 'alert(myB.Unassigned_Instructors.instructor[0].name);';
 var tt = document.createTextNode(scr);
 ss.appendChild(tt);
 var hh = document.getElementsByTagName('head')[0];
 hh.appendChild(ss);
 alert(myB.Unassigned_Instructors[0].name);

}

(...)

if(xmlHttp.readyState === 4) {
var raw_result = xmlHttp.responseText;
alert(refined_result = raw_result.match('{\.*}'));

var myJSONObject = eval("("+ refined_result +")");

alert(myJSONObject.Unassigned_Instructors[0].name);
}
 
P

pranny

With some experiments, i managed to get something
if(xmlHttp.readyState==4){
 var raw_result = xmlHttp.responseText;
 refined_result = raw_result.match('{\.*}');
 var myJSONObject = refined_result;
 var ss = document.createElement('script');
 var scr = 'var myB = '+ myJSONObject + ';';
 scr = scr + 'alert(myB.Unassigned_Instructors.instructor[0].name);';
 var tt = document.createTextNode(scr);
 ss.appendChild(tt);
 var hh = document.getElementsByTagName('head')[0];
 hh.appendChild(ss);
 alert(myB.Unassigned_Instructors[0].name);

(...)

if(xmlHttp.readyState === 4) {
  var raw_result = xmlHttp.responseText;
  alert(refined_result = raw_result.match('{\.*}'));

  var myJSONObject = eval("("+ refined_result +")");

  alert(myJSONObject.Unassigned_Instructors[0].name);

}

Thanks Jorge. This is exactly what i had been looking for.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top