Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Ext.decode and arrays

  1. #1
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324
    Vote Rating
    2
      0  

    Default Ext.decode and arrays

    I'm using the following line of code to decode some json data:

    var jsonData = Ext.util.JSON.decode(response.responseText);

    The data being returned looks something like this {"Rows":[ "RowNum":1", ......

    My problem is that when I do jsonData.Rows[0].RowNum, it is telling me that Rows is undefined.

    What is the correct way to use the decode method?

  2. #2
    Ext User
    Join Date
    Oct 2008
    Location
    Ger
    Posts
    178
    Vote Rating
    0
      0  

    Default

    Using Ext.decode is the right way. It can be something like this

    Code:
    Ext.Ajax.request({  
        url : 'url.php', 
        success: function( response) {  
            var serverResponse = Ext.util.JSON.decode(response.responseText);
            alert(serverResponse.test);
        }
    });
    but u need to be sure, that your JSON response from server is valid. With this JSON snippet the failure is hard to find....

  3. #3
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,197
    Vote Rating
    1002
      0  

    Default

    Well, from the sample you post, it's not valid json.

    Code:
    {
        Rows: [RowNum: 1] // <--- not valid
    }
    Code:
    {
        Rows: [{
            RowNum: 1
        }]
    }
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  4. #4
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324
    Vote Rating
    2
      0  

    Default

    The documentation says that if the json is invalid, there will be a syntax error thrown. So I know that the json is fine. This same json string works with jquery's jQuery.parseJSON so I'm positive there is no problems with the data.

    With jquery i can do this:

    var obj = jQuery.parseJSON(json);

    $.each(obj.Rows, function(i, ticket)
    {
    }

    But I thought the whole point of moving to sencha was going lightweight, and not having to include jquery.

  5. #5
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,197
    Vote Rating
    1002
      0  

    Default

    Well, you haven't show what your json is, so I can't really comment definitively. The more info you provide the more likely it is someone can help you.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  6. #6
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324
    Vote Rating
    2
      0  

    Default

    Here is the Json string.

    {"Rows":[ {"RowNum":"1","TicketId":"33371", "BusinessUnit":"null","Test Field":""}]}

  7. #7
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,197
    Vote Rating
    1002
      0  

    Default

    Ok, the JSON is valid. What is jsonData? Have you checked if it's null?

    For example:

    Code:
    var s = '{"Rows":[ {"RowNum":"1","TicketId":"33371", "BusinessUnit":"null","Test Field":""}]}';
    var o = Ext.decode(s);
    
    console.log(o.Rows[0].RowNum);
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  8. #8
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324
    Vote Rating
    2
      0  

    Default

    jsonData is not null. When I write out the value, it is the json string specified in the previous thread.

  9. #9
    Touch Premium Member
    Join Date
    Jun 2010
    Posts
    324
    Vote Rating
    2
      0  

    Default

    Well, this is strange. calling decode twice like the below works:

    var jsonData = Ext.decode(response.responseText);
    var s = jsonData;
    var o = Ext.decode(s);
    alert(o.Rows[5].TicketId);

  10. #10
    Ext User
    Join Date
    Oct 2008
    Location
    Ger
    Posts
    178
    Vote Rating
    0
      0  

    Default

    somethin like this dirty code^^ should do it
    i just tested it and it works on my dev environment

    Code:
                            Ext.Ajax.request({  
                                url : serverURL, 
                                success: function(objServerResponse){  
                                    var serverResult = Ext.util.JSON.decode(objServerResponse.responseText);
                                    
                                    for (var a = 0; a < serverResult.Rows.length; a++) {
                                        var row = serverResult.Rows[a];
                                        alert(row.TicketId);
                                    }
                                    //alert(serverResult.Rows[0].TicketId);
                                }
                            });

Page 1 of 2 12 LastLast

Similar Threads

  1. I wonder whether Ext changes javascript arrays?
    By vocabo in forum Ext 3.x: Help & Discussion
    Replies: 14
    Last Post: 1 Nov 2009, 7:05 AM
  2. scope used for Ext.decode (Ext.util.JSON.decode)
    By conorarmstrong in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 1 Jun 2009, 12:29 PM
  3. Ext.decode() and arrays
    By Dumas in forum Ext 2.x: Help & Discussion
    Replies: 12
    Last Post: 29 Apr 2009, 10:48 AM

Posting Permissions

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