0

I have an API that returns this in JSON:

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"cost": 1000, "amount": "$10 Gift Card", "id": 2, "resource_uri": "/api/amount/2/", "slug": "10-gift-card"}]}

When I'm trying to parse it in jQuery, I can access the meta values just fine, but I'm having trouble accessing the values in objects. To be specific, I need to have access to "cost" and "amount" when I make a call to this api. Any help?

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
user3084860
  • 421
  • 5
  • 19
  • 3
    Did you try: `yourObject.objects[0].cost` – PSL Dec 18 '13 at 02:06
  • @PSL Thanks, was easy enough, lol. At the risk of asking for too much, what way would you recommend making the ajax call? I'm thinking of just using $.ajax() – user3084860 Dec 18 '13 at 02:32

2 Answers2

0

Working demo http://jsfiddle.net/QS2FB/

Hope rest fits your needs :)

Code

var data = '{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"cost": 1000, "amount": "$10 Gift Card", "id": 2, "resource_uri": "/api/amount/2/", "slug": "10-gift-card"}]}';

var parsed = JSON.parse(data);

$(parsed).each(function (i) {
    alert(parsed.objects[i].cost);
});
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
0

assuming you are using $.ajax the response will be parsed for you!

$.ajax({
  url: '/yourservice.json',
  success: function(data){
      console.log(data.meta.limit) //logs '20'
  },
});

http://api.jquery.com/jQuery.ajax/

actual_kangaroo
  • 5,971
  • 2
  • 31
  • 45