...

View Full Version : How to handle JSON Response?



Dan06
07-28-2009, 05:20 PM
Why does one need to use eval() in order to 'handle' a javascript array? And are there any alternatives?

As a server response, I receive a JSON array: i.e. [["Concrete", "10", "10", "100"]]. Originally, I was directly assigning this response to a variable. While I was able to traverse the array with:



var ex = [["Concrete", "10", "10", "100"]];
document.write(ex[0][0]);


I was NOT able to use for loops to traverse the array UNLESS I first used eval:


var ex = [["Concrete", "10", "10", "100"]];
for (var i = 0; i < ex.length; i++){
for (var j = 0; j < ex[i].length; j++){
document.write(ex[i][j]);
/** Above does not work */
}
}

var ex = eval([["Concrete", "10", "10", "100"]]);
for (var i = 0; i < ex.length; i++){
for (var j = 0; j < ex[i].length; j++){
document.write(ex[i][j]);
/** Above does work */
}
}

ckeyrouz
07-28-2009, 05:50 PM
try putting your data this way:

var ex = [{"Concrete", "10", "10", "100"}];


instead of this way:

var ex = [["Concrete", "10", "10", "100"]];


the only thing is I replaced the inner "[]" with "{}"

smokinguns
07-28-2009, 10:36 PM
You use eval to convert the server side response to a object. Eval is fine as long as the response is from a trusted server. I use json2.js from
http://www.json.org/js.html to parse(response from server) and stringify strings(for sending to server).

rnd me
07-28-2009, 11:21 PM
try putting your data this way:

var ex = [{"Concrete", "10", "10", "100"}];



that's just plain wrong, no nicer way of saying it.
Objects require key:"value" pairs, not indexed primitives.


eval is not doing anything there, eval is made to work on strings, not objects or arrays...


OP:
the original posts works fine for me, what are you expecting/trying to get?

ckeyrouz
07-29-2009, 08:56 AM
I think it was a quick answer from my side because at first sight and even without testing it I knew that the original code is working and I shouldn't have done that.

But Dan06's problem was that he was not able to do so because the data is not written like this in javascript but was pushed towards javascript from the server side and in order to activate it as an array in the javascript is to use the eval method.

Without eval the value of the object will be a normal string.

I would have answered this post in a correct way if I took my time in reading the question and I apologize for that and I think that all of us should read the question before answering.

For this post smokinguns has the only accurate answer.

Dan06
07-29-2009, 05:14 PM
OP:the original posts works fine for me, what are you expecting/trying to get?

@rnd_me: My php script returns a json encoded value. I wanted to assign this value (array) to a js object property, which would then create a mutable table from the value (array). When I directly assigned the responseText value, the javascript code seemingly read the array as a string, thus incorrectly creating the table.

ckeyrouz's post, which is quoted below, hits the nail on the head. I apologize if my example was misleading/confusing.


But Dan06's problem was that he was not able to do so because the data is not written like this in javascript but was pushed towards javascript from the server side and in order to activate it as an array in the javascript is to use the eval method.

Without eval the value of the object will be a normal string.

rnd me
07-29-2009, 09:29 PM
@rnd_me: My php script returns a json encoded value. I wanted to assign this value (array) to a js object property, which would then create a mutable table from the value (array). When I directly assigned the responseText value, the javascript code seemingly read the array as a string, thus incorrectly creating the table.


That makes sense; it just didn't look like a string in the example, so eval didn't make sense to me at the time. Glad it's working and that you found your answer.

If you are handling server-returned json, you might want to consider jsonp instead of ajax. It's a little easier to work with, especially when doing multiple updates. You don't need to use eval with it. It also gets to cross domains, which can ease deployment headaches associated with ajax's same-origin policies.

If you are building tables from json, you might want to check out this thread (http://www.codingforums.com/showthread.php?t=172949), if you want an easy way to build user-friendly grid from raw data.

Cheers!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum