1. #1
    Sencha User
    Join Date
    May 2007
    Posts
    52
    Vote Rating
    0
    dario is on a distinguished road

      0  

    Default Handling json null

    Handling json null


    Hi all
    I'm using a Store with a Json reader to get some data from the backend. Everything works fine but I get an error when some of this data has a null value.
    Here's my data

    PHP Code:

    {"licences":[{"Id":2,"LicenseKey":"123456","Point":null,"Owner":{"Id":1,"Name":"Witbit"}, "Mac":null,"Active":false},
    {
    "Id":3,"LicenseKey":"789013","Point":null,"Owner":null"Mac":null,"Active":false}]} 
    and corresponding reader:

    PHP Code:

    this
    .DS  = new Ext.data.Store(
    {
        
    reader: new Ext.data.JsonReader(
                { 
                    
    root"licences",
                    
    fields: [ "Id""LicenseKey", {name:"Owner_Name"mapping:"Owner.Name"}, "Point",  "Mac""Active" 
                })
    }); 
    When I load the data I get an error because the second license object has no owner (=null) and thus Owner.Name is not an object.
    I already found a way to make my app work but I wonder if there's way to handle data with null value...for example the reader should check if an object is null before trying to parse its members?
    Thanks in advance

  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,006
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Pretty much, you'd have to implement some kind of custom parser with try catch blocks.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  3. #3
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    94
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Try:

    Code:
    {name:"Owner_Name", mapping:"Owner", convert: function(v) {return v ? v.Name : null;}}

  4. #4
    Sencha User
    Join Date
    May 2007
    Posts
    52
    Vote Rating
    0
    dario is on a distinguished road

      0  

    Default


    Thanks Condor, that perfectly worked!

Thread Participants: 2