【问题标题】:Loading Big Cartel JSON object in to external PHP将 Big Cartel JSON 对象加载到外部 PHP
【发布时间】:2014-04-16 01:14:59
【问题描述】:

我们对 json 相当缺乏经验,并且无法将来自 Big Cartel 商店的 json 字符串中的对象显示到我们的 WordPress 网站。

所以,如果在我们的 Big Cartel 网站上,我们会加载以下 URL(这只是指向商店购物车位置的链接,末尾带有 .json):

例如ourstore.com/cart.json

我们得到以下变量字符串:

{"items":[{"id":59269479,"name":"Test 4","price":"8.0","unit_price":"4.0","shipping":0.0,"tax":0.0,"total":"4.0","quantity":2,"product":"test-4","option":59269479},{"id":59269434,"name":"Test 2","price":"2.0","unit_price":"1.0","shipping":0.0,"tax":0.0,"total":"1.0","quantity":2,"product":"test-2","option":59269434},{"id":59271363,"name":"Test 6","price":"544.0","unit_price":"544.0","shipping":0.0,"tax":0.0,"total":"544.0","quantity":1,"product":"test-6","option":59271363}],"item_count":5,"price":"554.0","total":"554.0"}

从该字符串中,我们基本上只是尝试在外部 WordPress 网站中显示 "item_count":5 变量。

如果有人有任何想法,将不胜感激!谢谢!

【问题讨论】:

    标签: json wordpress api variables bigcartel


    【解决方案1】:

    很遗憾,您尝试在外部 Wordpress 网站上使用的 javascript API 无法处理购物车数据——购物车信息只能直接显示在 Big Cartel 商店中。更多信息在这里:http://help.bigcartel.com/customer/portal/articles/772751-javascript-ajax

    您可以使用我们的 API 在您的网站上显示其他商店和产品信息,更多详细信息和简要教程在这里:http://help.bigcartel.com/customer/portal/articles/772771-api

    【讨论】:

      【解决方案2】:

      请在 javascript 中使用此代码。

      var jsonString = '{"items":[{"id":59269479,"name":"Test 4","price":"8.0","unit_price":"4.0","shipping":0.0,"tax":0.0,"total":"4.0","quantity":2,"product":"test-4","option":59269479},{"id":59269434,"name":"Test 2","price":"2.0","unit_price":"1.0","shipping":0.0,"tax":0.0,"total":"1.0","quantity":2,"product":"test-2","option":59269434},{"id":59271363,"name":"Test 6","price":"544.0","unit_price":"544.0","shipping":0.0,"tax":0.0,"total":"544.0","quantity":1,"product":"test-6","option":59271363}],"item_count":5,"price":"554.0","total":"554.0"}';
      var jsonObj = JSON.parse(jsonString);
      

      现在只需在要显示的地方使用以下行

      jsonObj.item_count
      

      【讨论】:

      • 感谢您的回复 dileep,但我尝试此操作时没有显示任何内容。 'jsonObj.item_count' 是否需要包含在 以外的任何内容中?还是它还需要某种写入或打印功能? (另外,如果我们从动态 url 加载 json,我们是否只需将上面的 json 变量替换为 URL?
      • 你能提供你如何得到这个json的代码
      • 我将其更改为使用以下方法,这似乎可以正常工作:<span id="bagcount"></span> <script> var JSONObject = {"items":[],"item_count":5,"price":0,"total":0}; document.getElementById("bagcount").innerHTML=JSONObject.item_count; </script> 但唯一的是我们需要动态加载 JSONObject:store.nuswimwear.com/cart.json
      • 你在用jquery吗
      • 我认为这会对你有所帮助 jQuery.getJSON( "store.nuswimwear.com/cart.json", function( data ) { var jsonObj = JSON.parse(data); document.getElementById("bagcount").innerHTML= jsonObject.item_count });
      猜你喜欢
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多