-1

I want to get JSON string to my Jquery Mobile App and add that values in to Java-Script array for future use.

here is the structure of the json

{"daily":[{"a":1,"b":3,"c":2,"d":5,"e":3}]}

here is the link for above daily_report

here is my code

 $(document).on('pagebeforeshow', '#pageone', function(e, data){      
 $.ajax({url: "http://iilsfa.br0s.info/Dashboard/get_daily_report.php",
    dataType: "json",
    async: true,
    success: function (result) {
        ajax.parseJSONP(result);
    },
    error: function (request,error) {
        alert('No Network!');
    }
  });         
 });


 var ajax = {  
parseJSONP:function(result){
    $.each( result, function(i, row) {
        $('#output').append('<li><h3>' + row.a+ '</h3></li>');
          // I Need to add json elements to an array here
    });
    $('#output').listview('refresh');
  }
}
  • #output is a tag located in #pageone

Above code is not working.please show me how to fix it..thank you..

san88
  • 1,286
  • 5
  • 35
  • 60
  • 1
    You have an odd way to name your function. It doesn't do any parsing at all and the response doesn't seem to be JSONP. Anyways, you should better explain what "is not working". Which output do you get? Do you get any errors? Which output do you expect? You should have a look at [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196). – Felix Kling Jun 10 '14 at 06:35
  • adding to an array is not implemented in my code..not working means when i run the app it displays Undefined Object.. iam hoping to get java-script array form this function..thanks for your responce – san88 Jun 10 '14 at 06:39
  • Well, you are iterating over the `result` object, which means `row` *is* the array. And arrays don't have an `a` property. It seems like you want to iterate over `result.daily` instead. Again, have a look at http://stackoverflow.com/q/11922383/218196. – Felix Kling Jun 10 '14 at 06:41

1 Answers1

0
$.ajax({url: "http://iilsfa.br0s.info/Dashboard/get_daily_report.php",
    dataType: "json",
    async: true,
    success: function (result) {
        result.daily.forEach(function(el){  
          $('#output').append('<li><h3>' + el.a+ '</h3></li>'); 
        });

    },
    error: function (request,error) {
        alert('No Network!');
    }
  });         
 });
Pranay Soni
  • 1,056
  • 10
  • 18
  • You might want to explain what you changed and why. That would make the answer much more helpful. – Felix Kling Jun 10 '14 at 06:43
  • thanks Soni..your code is working.it display correct number under
    .how to push these json values to java-script array..
    – san88 Jun 10 '14 at 06:44
  • @san88: jQuery already parsed the JSON. `result` is a JavaScript object and `result.daily` is a JavaScript array. – Felix Kling Jun 10 '14 at 06:50
  • I had forgot to enter comment line under for loop. that's why i edit the post. – san88 Jun 10 '14 at 06:51
  • thanks Felix ..im new to java script and JQM..now its working..again thanks for your help.. – san88 Jun 10 '14 at 06:55