JSON Question

Thread is closed for posting
3 posts, 1 answers
  1. Sam
    Sam avatar
    14 posts
    Member since:
    Apr 2011

    Posted 04 Sep 2013 Link to this post

    I'm new to JS/JSON and trying to put an app together but I am running into an issue parsing the returned JSON.
    I am getting the data back from Everlive using Alert(JSON.Stringify(data))  but I cannot seem to get the syntax right when I try to parse the data to update the UI.  If I set this.tankOwner.value to a literal it works (this.tankOwner.value = "SomeValue"). 

    Thanks very much for any assistance

    Code as follows:

    _getTankByID: function(message) {
          
            var filter = new Everlive.Query();
            filter.where().eq('SerialNo', message);
            var data = Everlive.$.data('Tank');
           
            data.get(filter)
                .then(function(data) {
                   // alert(JSON.stringify(data));
                   
                   var tankResp = JQuery.parseJSON(data);
                   this.tankOwner.value = tankResp.TankOwner   
                   
                    
                },
                function(error){
                    alert(JSON.stringify("Please Login"));
                });
           
            
        }

  2. Answer
    Ivan Pelovski
    Admin
    Ivan Pelovski avatar
    16 posts

    Posted 05 Sep 2013 Link to this post

    Hello,

    When you retrieve multiple items from Everlive the result object is already parsed and has 2 fields: 'count' and 'result'. The 'count' field keeps the total number of items in the type that fulfill the filter criteria, and the 'result' field contains the array of items. So your code may look something like this:
    _getTankByID: function(message) {
        var filter = new Everlive.Query();
        filter.where().eq('SerialNo', message);
        var data = Everlive.$.data('Tank');
         
        data.get(filter)
            .then(function(res) {
               if (res.count > 0) { // checks if there is an item that fulfills the filter criteria
                   var tankResp = res.result[0];
                   this.tankOwner.value = tankResp.TankOwner;
               }
               else {
                // display error
               }
            },
            function(error){
                alert(JSON.stringify("Please Login"));
            });
    }
    Please let us know if you have other problems or questions.

    Regards,
    Ivan Pelovski
    Telerik

    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Share feedback and vote for features on our Feedback Portal.
  3. Sam
    Sam avatar
    14 posts
    Member since:
    Apr 2011

    Posted 05 Sep 2013 Link to this post

    Ivan, Thanks Very Much!

    I was banging my head on this one - That worked!
Back to Top