1. #1
    Sencha User
    Join Date
    May 2010
    Posts
    111
    Vote Rating
    1
    sajan is on a distinguished road

      0  

    Default Problem decoding JSON string

    Problem decoding JSON string


    Hi,
    I am facing some problem decoding JSON String.
    I am sending this JSON
    {\"success\":true,\"data\":[{\"key\":"+String(113556)+"}]}

    but I dont know how to decode it on success function.

    every time it shows error.
    Can you please suggest how to decode JSON String.

    thanks in advance

  2. #2
    Sencha Premium Member
    Join Date
    Nov 2007
    Location
    Moore, Oklahoma
    Posts
    464
    Vote Rating
    13
    Mthor will become famous soon enough

      0  

    Default


    Code:
    success: function (response, opts) {
    	 var obj = Ext.decode(response.responseText);
    }

  3. #3
    Sencha User
    Join Date
    May 2010
    Posts
    111
    Vote Rating
    1
    sajan is on a distinguished road

      0  

    Default


    Hi,
    I already did till that.
    I wanted to get the values.
    If I do console.log(obj) it still says undefined.

    How to get the values out of it?
    Please do let me know
    thanks

  4. #4
    Sencha Premium Member
    Join Date
    Nov 2007
    Location
    Moore, Oklahoma
    Posts
    464
    Vote Rating
    13
    Mthor will become famous soon enough

      0  

    Default


    Code:
     success: function (response, opts) {
        var obj = Ext.decode(response.responseText);
        var objCount = obj.totalCount;
    
        var name = obj.items[0].name; //this is what I use cause I return items, 
    
        var key= obj.data[0].key;  // I think this would be what you would use
    
    
    }
    also your json i don't think should have those slashes in there. maybe you are getting bad json back

    ex. {"totalCount":1,"items":[{"number":"374432"}]}

  5. #5
    Sencha User
    Join Date
    May 2010
    Posts
    111
    Vote Rating
    1
    sajan is on a distinguished road

      0  

    Default


    Hi,
    My JSON is being returned as
    Code:
    {"success":true, "totalCount":1,"items":[{"name":1334163657}]} (I followed your suggestion)
    but still the code lines
    Code:
    var objCount = obj.totalCount;  
    
    
    and 
        var name = obj.items[0].name; //this is what I use cause I return items,
    is showing error.
    It is showing errors as

    Uncaught TypeError: Cannot read property 'totalCount' of undefined

    &
    Uncaught TypeError: Cannot read property 'items' of undefined

    I am still unable to figure out the error. Searched a lot of place but no solution till now!! Please help
    thanks


  6. #6
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,198
    Vote Rating
    120
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    Use a debugger.

    Stop on errors, what is obj? Is it still a string at that point? Does it need to be converted to an object?
    Aaron Conran
    @aconran
    Sencha Architect Development Team

  7. #7
    Sencha User
    Join Date
    May 2010
    Posts
    111
    Vote Rating
    1
    sajan is on a distinguished road

      0  

    Default


    Hi,
    Yes !! debugging it.
    On Place where it shows error
    Code:
    success: function(response, result) {
     var responseData = Ext.decode(response.responseText);/// it says responseData is undefined screenshot attached
     var objCount = responseData.totalCount;
            var name = obj.items[0].name; 
    Ext.Msg.alert(name)
    So after that all lines failed. I am new to this. Previously while doing extjs I used firebug but now since sencha is not compatible with chrome. I am facing trouble all the way.
    Cannot procede until I resolve this.
    So please help me
    thanks
    Attached Images

  8. #8
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,198
    Vote Rating
    120
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    So whats the value of response.responseText?
    Aaron Conran
    @aconran
    Sencha Architect Development Team

  9. #9
    Sencha User
    Join Date
    May 2010
    Posts
    111
    Vote Rating
    1
    sajan is on a distinguished road

      0  

    Default


    Nothing,
    But I just figured it out

    I just did this
    Code:
     var name = result.items[0].name;
    
            Ext.Msg.alert('success',name);
    and it showed the value

    So, I guess it worked!!!

    thanks

  10. #10
    Sencha Premium Member
    Join Date
    Nov 2007
    Location
    Moore, Oklahoma
    Posts
    464
    Vote Rating
    13
    Mthor will become famous soon enough

      0  

    Default


    glad you got it working!!

    but you said "sencha is not compatible with chrome." Chrome is all I use to debug senchatouch and extjs4

    I suggest chrome as a debugging tool

Thread Participants: 2