如何通过Json和Jquery获取bitcoincharts加权价格?

发布于 2024-12-09 02:39:05 字数 732 浏览 1 评论 0原文

我试图让 Jquery 读取 json 文件,

但不幸的是我无法让它工作。

这是我的 json 文件。请参阅:http://bitcoincharts.com/t/ Weighted_prices.json

{
    "USD": {"7d": "4.4549", "30d": "5.2016", "24h": "4.1373"}, 
    "GBP": {"7d": "2.9706", "30d": "3.2620", "24h": "2.5463"}, 
}

我的 jquery 脚本看起来像这样

<script type="text/javascript">
 (document).ready(function(){
       $.getJSON('http://bitcoincharts.com/t/weighted_prices.json',function(data){
               $("#results").html(data[0].30d);
       });
});
</script>

我尝试抓取 USD>30d>5.2016

I'm trying to get Jquery to read a json file,

But unfortunately I can not get it to work.

This is my json file. see: http://bitcoincharts.com/t/weighted_prices.json

{
    "USD": {"7d": "4.4549", "30d": "5.2016", "24h": "4.1373"}, 
    "GBP": {"7d": "2.9706", "30d": "3.2620", "24h": "2.5463"}, 
}

My jquery script looks like this

<script type="text/javascript">
 (document).ready(function(){
       $.getJSON('http://bitcoincharts.com/t/weighted_prices.json',function(data){
               $("#results").html(data[0].30d);
       });
});
</script>

I try to grab USD>30d>5.2016

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-12-16 02:39:05

获取美元 -> 30d (= 5.2016) 你需要这样做:

data["USD"]["30d"]

你不能说 data[0] 因为使用数字索引(通常)仅适用于数组,而你没有数组。您有一个具有两个属性“USD”和“GBP”的对象,并且每个属性都有一个具有属性“7D”、“30d”、“24h”的对象。

更详细地说:

JavaScript array 文字声明使用仅包含元素列表的方括号,如下所示:

var myArray = ["a","b","c"];

但是 object 文字声明使用带有键值对的大括号,如下所示您在问题中提供的 JSON,或者这里有一个更简单的示例:

var myObject = { "key1" : "value1", "key2" : "value2", "key3" : "value3" };

“技巧”是,一旦定义,数组和对象都可以使用方括号表示法进行访问,但数组使用数字索引,而对象使用字符串键。

您还可以像您尝试做的那样使用点表示法,但仅限于具有符合有效 JavaScript 标识符规则的键名称的属性,即不以数字开头、没有空格、不是保留字等。这些限制只适用于点表示法,所以如果您使用方括号表示法,您可以有空格、数字等。

嵌套数组和对象很好,包括混合两者,但在您的特定情况下,您只有一个包含另外两个对象的对象对象,没有数组。

因此 data["USD"] 将为您提供 {"7d": "4.4549", "30d": "5.2016", "24h": "4.1373"}

data["GBP"] 将为您提供 {"7d": "2.9706", "30d": "3.2620", "24h": "2.5463"}

您可以使用 data["USD"]["30d"] 将其范围缩小到您想要的单个值。

编辑:由于 Ajax 同源策略,您的 $.getJSON 请求将不起作用,即,您只能向与当前页面相同的域发出 JSON 请求.

你可以尝试 JSONP - jQuery 支持它,而你几乎不需要付出任何努力:你只需要将 ?callback=? 添加到 url 的末尾 - 除了 JSONP 需要支持服务器,它看起来像比特币人民不支持它。

因此,最简单的方法是在服务器端代码中获取数据,这样同源策略就不会妨碍您。

To get USD -> 30d (= 5.2016) you need to do this:

data["USD"]["30d"]

You can't say data[0] because using a numeric index is (generally) only applicable to arrays and you don't have an array. You have an object with two properties, "USD" and "GBP", and each of those properties has an object with the properties "7D", "30d", "24h".

In more detail:

JavaScript array literal declarations use square brackets that contain simply a list of elements, like this:

var myArray = ["a","b","c"];

But object literal declarations use curly brackets with key-value pairs like in the JSON you provided in your question, or here's a simpler example:

var myObject = { "key1" : "value1", "key2" : "value2", "key3" : "value3" };

The "trick" is that once defined both arrays and objects are accessed with the square bracket notation, but arrays use numeric indexes and objects use string keys.

You can also use dot notation like you were trying to do, but only on properties with a key name that meets the rules of valid JavaScript identifiers, i.e., not starting with a number, no spaces, not a reserved word, etc. These restrictions only apply to dot notation though, so if you use the square bracket notation you can have spaces, numbers, etc.

It's fine to nest arrays and objects, including mixing the two, but in your particular case you just had an object containing two other objects, no arrays.

So data["USD"] will give you {"7d": "4.4549", "30d": "5.2016", "24h": "4.1373"}.

data["GBP"] will give you {"7d": "2.9706", "30d": "3.2620", "24h": "2.5463"}.

You narrow it down to the individual value you want with data["USD"]["30d"].

EDIT: Your $.getJSON request isn't going to work because of the Ajax same-origin policy, i.e., you are only allowed to make JSON requests to the same domain as the current page's.

You could try JSONP - which jQuery supports with practically no effort on your part: you just have to add ?callback=? to the end of the url - except that JSONP requires support on the server and it looks like the bitcoin people don't support it.

So the easiest way forward is to get the data in your server side code where the same-origin policy won't get in your way.

寄意 2024-12-16 02:39:05

我目前很难找到引用,但问题是您无法通过点表示法访问密钥,您需要通过数组表示法访问它。就你而言...

data[0]["30d"]

I'm having a hard time finding the reference at the moment, but the problem is that you can't access the key via dot notation, you need to access it via array notation. In your case...

data[0]["30d"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文