Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to handle JSON Response?

    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:

    Code:
    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:
    Code:
    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 */
    }
    }
    Last edited by Dan06; 07-28-2009 at 04:46 PM.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    try putting your data this way:
    Code:
    var ex = [{"Concrete", "10", "10", "100"}];
    instead of this way:
    Code:
    var ex = [["Concrete", "10", "10", "100"]];
    the only thing is I replaced the inner "[]" with "{}"

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by ckeyrouz View Post
    try putting your data this way:
    Code:
    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?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd_me
    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.

    Quote Originally Posted by ckeyrouz View Post
    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.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by Dan06 View Post
    @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, if you want an easy way to build user-friendly grid from raw data.

    Cheers!
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •