This is a migrated thread and some comments may be shown as answers.

JSON Question

2 Answers 85 Views
jQuery Mobile
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sam
Top achievements
Rank 1
Sam asked on 04 Sep 2013, 02:34 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Pelovski
Telerik team
answered on 05 Sep 2013, 11:56 AM
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.
0
Sam
Top achievements
Rank 1
answered on 05 Sep 2013, 12:04 PM
Ivan, Thanks Very Much!

I was banging my head on this one - That worked!
Tags
jQuery Mobile
Asked by
Sam
Top achievements
Rank 1
Answers by
Ivan Pelovski
Telerik team
Sam
Top achievements
Rank 1
Share this question
or