1

I would like to know how to print a json object with a specific id to a div?! My json file looks like this (shortened) and is automatically generated by a webshop program I use:

 "shipping":{
   "methods":{
     "core|13490|40699":{
       "id":"core|13490|40699",
       "price":{
         "price_excl":"0.0000","price_incl":"0.0000"
       }
     },
     "core|10292|40695":{
       "id":"core|10292|40695",
       "price":{
         "price_excl":"21.0084","price_incl":"25.0000"
       }
     }
   }
 },

and my script like this:

        window.onload = function(){
      $.getJSON('http://shop.com/cart/?format=json', function(data){
        $.each(data.cart.shipping.methods, function(index, method){
          $('<span></span>')
            .html('<strong>' + method.price.price_incl + '</strong>')
            .appendTo('.cart-shipping');
        });
      });
    };

What i try to archieve is that only the shipping price of shipping method with id "core|10292|40695" is shown. With the code I have both shipping prices are shown. Further I would like to know how to format those prices into ' real' prices instead of 25.000.

I'm pretty new to JSON, jquery/ajax as you probably can see, but I'm willing to learn. The above I archieved with searching this site but my question is something I really can't figure out.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Meules
  • 1,349
  • 4
  • 24
  • 71

3 Answers3

1

Have you tried using a simple if statement? Eg:

if (index == "core|10292|40695") {
    $('<span></span>').html('<strong>' + method.price.price_incl + '</strong>').appendTo('.cart-shipping');
}

In regards to formatting the currency, I'm sure there's plenty of information available

Community
  • 1
  • 1
Christian
  • 19,605
  • 3
  • 54
  • 70
  • thanks! This works. Is it also possible to have something like -> if (index == "blabla") or (index == " blabla2") etc.? – Meules May 28 '12 at 02:51
  • Yes, you can do that like `if (index == "core|10292|40695" || index == "bla2")` etc. – Christian May 28 '12 at 02:59
  • When I do this it works. Thanks for that! Only problem is that he now shows the correct "core" id but also a "core" id I actually don't want to display. See top of the page, he now shows both and when I only use if (index == "core|10292|40695") instead of (index == "core|10292|40695" || index == "bla2") etc he shows correct. Any thoughts on this? – Meules May 28 '12 at 15:50
1

What you want to do is replace the following block:

$.each(data.cart.shipping.methods, function(index, method){
  $('<span></span>').html('<strong>' + method.price.price_incl + '</strong>').appendTo('.cart-shipping');
});

with:

$('<span></span>').html('<strong>' + data.cart.shipping.methods['core|10292|40695'].price.price_incl + '</strong>').appendTo('.cart-shipping');

Basically, the original code is iterating thru the received data with the $.each construct. In your case, you don't want to iterate thru all the values, you already know which one you want.

Also, beware of the answers that suggest you use an if statement. These answers will still be iterating thru the whole object, wasting resources.

ziad-saab
  • 19,139
  • 3
  • 36
  • 31
  • I'll give this a try tomorrow! – Meules May 28 '12 at 02:53
  • Does this also work with some sort of if statement? So when my cart total price is 100 show shipping method core|10292|40695 or when it's 1000 show shipping method core|0292|50695 etc.? – Meules May 28 '12 at 13:15
  • Yes. Depending on the format of the output, you have to check the price in `data.cart`. If you show an example output of `http://shop.com/cart/?format=json` then I can give you a more complete answer. – ziad-saab May 28 '12 at 13:17
  • I totally missed your feedback. This is the complete json of the part that is relevant: – Meules May 28 '12 at 21:02
-1

$('<span></span>').html("whatever") will create a span containing such data but the span is not yet shown on the page.

try to have a container on the page and insert data in it.

eg.

html: <div id="a"></div>

js: <script>$('#a').html('test');</script>

Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37