繁体   English   中英

检查json字符串中是否存在变量

[英]check if variable exist in json string

我有一个问题,我无法找到自己所以任何帮助赞赏。

我有一个动态购物车,里面装满了一些JSON数据。 在购物车中,运费也来自相同的json数据。 当方法可用时,脚本本身可以工作。 当该国家/地区的送货方式不可用时,脚本应该说“不可用”或类似的东西。 相反,它返回一个未定义的错误,导致购物车无法工作。 经过一些搜索,我发现当用户来自国家A(由购物车支持)时,变量“methods”出现在Json字符串中。 当用户来自国家B(不受支持)时,json字符串中不存在变量“methods”。 显然会导致错误。 所以我试图实现的是检查变量“方法”是否存在。 我找到了一些答案,比如使用typeof,HasOwnProperty,lenght()等,但我无法弄清楚我应该如何在下面的脚本中实现它。

我的剧本

 <script type="text/javascript">

        function getPriceDynamicCart(price){
          price = parseFloat(price).toFixed(2);
            price = '€ ' + price.replace('.', ',');
          return price;
        }
        function loadDynamicCart(){
          var url = "{{ '/cart/' | url }}?format=json";

          $.getJSON(url,function(data){

            var cartHtml = '';
            var priceTotal = 0;
            var foundShipment = false;

             $.each(data.cart.products, function(index, product){                  

              priceTotal = priceTotal + parseFloat(product.price.price_incl);
              productTitleLimit = jQuery.trim(product.fulltitle).substring(0, 19);

              cartHtml = cartHtml +
                '<div class="cart-product"><div class="cart-quantity">' +product.quantity+'&nbsp;x&nbsp;</div><div class="cart-title"><a href="'+ product.url +'" alt="'+ product.fulltitle + '" title="'+ product.fulltitle + '">' + productTitleLimit + '</a></div><div class="cart-price">' + getPriceDynamicCart(product.price.price_incl) + '</div></div>';
            });

            $.each(data.cart.shipping.methods, function(index, method){

              if(!foundShipment && !method.cod && !method.pickup) {
                priceTotal = priceTotal + parseFloat(method.price.price_incl);
                cartHtml = cartHtml +
                  '<div class="cart-shipping"><a class="vracht" style="cursor:pointer;" rel="nofollow">Verzendkosten:<br />(Vervallen bij afhalen)</a><div class="cart-price"> ' + getPriceDynamicCart(method.price.price_incl) + '</div></div>';

                foundShipment = true;                
              }
            });

            cartHtml = cartHtml +
              '<div class="cart-total"><div class="total">Totaal(incl. btw): <span>' + getPriceDynamicCart(priceTotal) + '</span></div>';

            $('#dynamic_cart').html(cartHtml);

          });
        }

        $().ready(function(){
          loadDynamicCart();
        });
      </script>
if(data.cart.shipping.methods !== undefined){

    $.each(data.cart.shipping.methods, function(index, method){

          if(!foundShipment && !method.cod && !method.pickup) {
            priceTotal = priceTotal + parseFloat(method.price.price_incl);
            cartHtml = cartHtml +
              '<div class="cart-shipping"><a class="vracht" style="cursor:pointer;" rel="nofollow">Verzendkosten:<br />(Vervallen bij afhalen)</a><div class="cart-price"> ' + getPriceDynamicCart(method.price.price_incl) + '</div></div>';

            foundShipment = true;                
          }
        });
}else{
  cartHtml = cartHtml +
              '<div class="cart-shipping"><a class="vracht" style="cursor:pointer;" rel="nofollow">Verzendkosten:<br />(Vervallen bij afhalen)</a><div class="cart-price">**Anything you care to write**</div></div>';
}

这应该工作。

编辑,我添加了一个用于设置cartHtml变量的else语句,因此您可以更灵活地将来使用。

在调用each之前,您可以检查data.cart.shipping.methods属性是否为null / undefined:

if (data.cart.shipping.methods) {
    $.each(data.cart.shipping.methods, function(index, method){
    //...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM