Results 1 to 7 of 7

Thread: Is Eval needed here?

  1. #1
    Ext User
    Join Date
    Sep 2009
    Posts
    55
    Vote Rating
    0
      0  

    Default Is Eval needed here?

    Given this JSON:

    Code:
    {"result":
        {"f": 
            [
                {"x":"y"}
            ],
        "g":
            {
                "j":"k"}
            }
        }
    }
    The point is, 'f' contains a set of 'records', whereas 'g' contains a single value.

    It is no problem to parse 'f.x' out of here using a JsonReader, but what is the correct way to obtain the value of 'g.j'?

    This seems to work:

    eval(result).g.j;

    ... but as I understand it, 'eval' is to be avoided. Is this a case where it is unavoidable, or is there some other way to do this? I tried experimenting with different configurations of JsonReader as well as Ext.util.JSON.decode.

    Any advice appreciated.

  2. #2
    Sencha User hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,966
    Vote Rating
    18
      0  

    Thumbs up

    GregT:

    Ext.decode uses eval (unless Ext.USE_NATIVE_JSON == true) to parse your incoming JSON string into a object.

    Code:
    var o = Ext.decode( response.responseText);
    
    console.log( o.g.j ) ;
    Just stick with the standard Ext methods, and you'll be fine.
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  3. #3
    Ext User
    Join Date
    Sep 2009
    Posts
    55
    Vote Rating
    0
      0  

    Default

    Thank you Doug, but I tried that before and I get 'missing ] after element list'. This happens during the 'decode' statement.

    I am pretty sure my JSON is clean because 'eval', when called directly, parses it just fine (also, it looks fine visually and finally, it is being emitted by GSON which I have not yet seen screw anything up).

    So I have no clue what this error means. Any thoughts?

  4. #4
    Sencha User hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,966
    Vote Rating
    18
      0  

    Default

    This isn't right:

    Code:
     "g":
            {
                "j":"k"}
            }
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  5. #5
    Ext User
    Join Date
    Sep 2009
    Posts
    55
    Vote Rating
    0
      0  

    Default

    My apologies, that was a typo when I simplified my example to post it here.

    The full JSON is a fairly big chunk of text (too big to post here) with everything paired up properly, trust me

    Since 'eval' can handle it, shouldn't 'decode' be able to as well? I guess 'decode' does more than just run 'eval'...

  6. #6
    Sencha User VinylFox's Avatar
    Join Date
    Mar 2007
    Location
    Baltimore, MD
    Posts
    1,501
    Vote Rating
    8
      0  

    Default

    Its really quite straight forward...

    Code:
    doDecode = function(json){
        return eval("(" + json + ')');    
    }
    doDecode is what is called by decode if there is no native JSON support in the browser.

    Try running your full JSON through JSONLint.

  7. #7
    Sencha User hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,966
    Vote Rating
    18
      0  

    Default

    @Greg -- your incoming string should have CRLF encoded as \n, and embedded quotes need to be escaped as well ( "O\'brian" ).

    Good luck
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


Posting Permissions

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