By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,079 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,079 IT Pros & Developers. It's quick & easy.

Can anyone tell me how to use JSON data replied from server?

P: n/a
xhe
I found Jason is a very handy data format for client/server
communication. But I just met a problem as follows:

I want to read the data replied from server in Jason format, the reply
is like this:

[{"firstName":"Alessandro","lastName":"Crugnola","p hone":"+390332730999","email":"al********@sephirot h.it","_explicitType":"tutorials.Person"},
{"firstName":"Patrick","lastName":"Mineault","phon e":"+1234567890","email":"pa*****@5etdemi.com","_e xplicitType":"tutorials.Person"}]

it is generated automatically by amfphp1.9 from an array. I used Ajax
to call a method, and the server code replied an array in the above
json format.

Now comes the problem which I did a huge amount of google search and
can still not find any solution.

HOW CAN I USE THIS REPLY?

Ideally if we can read the reply in array method, it will be
excellent.

I tried this way:

var people= '{"result": '+ reply.responseText +"}";
alert (people.result[1].email)
But it failed.

If we use this to defina a json data

var people =
{"result":
[{"firstName":"Alessandro","lastName":"Crugnola","p hone":"+390332730999","email":"al********@sephirot h.it","_explicitType":"tutorials.Person"},
{"firstName":"Patrick","lastName":"Mineault","phon e":"+1234567890","email":"pa*****@5etdemi.com","_e xplicitType":"tutorials.Person"}]
}
Then I can use
alert ("people is "+ people.result[1].email);
to access the JSON record.

Can anyone tell me how to access the JSON data replied from server?

Thanks very much in advance.

Oct 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Oct 24, 3:44 pm, xhe <hexuf...@gmail.comwrote:
I found Jason is a very handy data format for client/server
communication. But I just met a problem as follows:

I want to read the data replied from server in Jason format, the reply
is like this:

[{"firstName":"Alessandro","lastName":"Crugnola","p hone":"+390332730999","email":"alessan...@sephirot h.it","_explicitType":"tutorials.Person"},
{"firstName":"Patrick","lastName":"Mineault","phon e":"+1234567890","email":"patr...@5etdemi.com","_e xplicitType":"tutorials.Person"}]

it is generated automatically by amfphp1.9 from an array. I used Ajax
to call a method, and the server code replied an array in the above
json format.

Now comes the problem which I did a huge amount of google search and
can still not find any solution.

HOW CAN I USE THIS REPLY?

Ideally if we can read the reply in array method, it will be
excellent.

I tried this way:

var people= '{"result": '+ reply.responseText +"}";
alert (people.result[1].email)
But it failed.

If we use this to defina a json data

var people =
{"result":
[{"firstName":"Alessandro","lastName":"Crugnola","p hone":"+390332730999","email":"alessan...@sephirot h.it","_explicitType":"tutorials.Person"},
{"firstName":"Patrick","lastName":"Mineault","phon e":"+1234567890","email":"patr...@5etdemi.com","_e xplicitType":"tutorials.Person"}]}

Then I can use
alert ("people is "+ people.result[1].email);
to access the JSON record.

Can anyone tell me how to access the JSON data replied from server?

Thanks very much in advance.
Try this:
var ajaxResponse = eval('(' + response.responseText + ')');
ajaxResponse.firstname.value;
or
ajaxResponse.firstname[0].value;
Hope that help

Oct 24 '07 #2

P: n/a
On Oct 24, 10:31 am, clbr <kalbrun.pa...@gmail.comwrote:
On Oct 24, 3:44 pm, xhe <hexuf...@gmail.comwrote:
I found Jason is a very handy data format for client/server
communication. But I just met a problem as follows:
I want to read the data replied from server in Jason format, the reply
is like this:
[{"firstName":"Alessandro","lastName":"Crugnola","p hone":"+390332730999","email":"alessan...@sephirot h.it","_explicitType":"tutorials.Person"},
{"firstName":"Patrick","lastName":"Mineault","phon e":"+1234567890","email":"patr...@5etdemi.com","_e xplicitType":"tutorials.Person"}]
it is generated automatically by amfphp1.9 from an array. I used Ajax
to call a method, and the server code replied an array in the above
json format.
Now comes the problem which I did a huge amount of google search and
can still not find any solution.
HOW CAN I USE THIS REPLY?
Ideally if we can read the reply in array method, it will be
excellent.
I tried this way:
var people= '{"result": '+ reply.responseText +"}";
alert (people.result[1].email)
But it failed.
If we use this to defina a json data
var people =
{"result":
[{"firstName":"Alessandro","lastName":"Crugnola","p hone":"+390332730999","email":"alessan...@sephirot h.it","_explicitType":"tutorials.Person"},
{"firstName":"Patrick","lastName":"Mineault","phon e":"+1234567890","email":"patr...@5etdemi.com","_e xplicitType":"tutorials.Person"}]}
Then I can use
alert ("people is "+ people.result[1].email);
to access the JSON record.
Can anyone tell me how to access the JSON data replied from server?
Thanks very much in advance.

Try this:
var ajaxResponse = eval('(' + response.responseText + ')');
ajaxResponse.firstname.value;
or
ajaxResponse.firstname[0].value;

Hope that help
That works, however, there are security implications involved should
the source be hijacked by a malicious attacker. Simply running the
JSON through "eval" opens the door to executing arbitrary code; JSON
is merely a data format and has no functions, so take a look at
http://www.json.org/js.html for methods to sanitize your data to
ensure it's safe before running it through "eval".

-David

Oct 24 '07 #3

P: n/a
xhe meinte:

[JSON Reply]
>
HOW CAN I USE THIS REPLY?
Douglas Crockford has written a parser for JSON responses to cope
security issues:

http://www.json.org/js.html
Gregor

--
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at ::: Forum für Multivisionsvorträge
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Oct 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.