JSON返回具有相同名称的多个值

我对此有点新,所以尽pipe事实上我听起来像是一个noob,但是对我来说不要太难。

我有一个JSON源,我从下面的JS代码拉:

$.getJSON("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=14", function(data) 

这给了我一个输出,如下所示:

 { "price": "0.00008926", "quantity": "304.08451708", "total": "0.02714258" }, { "price": "0.00008927", "quantity": "107.68391178", "total": "0.00961295" } 

我将其设置为var“结果”。

由于有多个“价格”的价值,我不知道如何使用第一个。 关于如何做到这一点的任何想法?

我使用Node.js和jQuery仅供参考。

你的JSON结构看起来像这样:

 { "success": 1, "return": { "WDC": { "marketid": "14", "label": "WDC\/BTC", "primaryname": "WorldCoin", "primarycode": "WDC", "secondaryname": "BitCoin", "secondarycode": "BTC", "sellorders": [{ "price": "0.00007760", "quantity": "2.79222406", "total": "0.00021668" }, { "price": "0.00007761", "quantity": "933.65491273", "total": "0.08358597" }, { "price": "0.00007842", "quantity": "7.39656299", "total": "0.00058004" }, ... and so on ... ] } } } 

因为JSON是为你自动parsing的,所以你只需要像普通的对象一样使用它。

 console.log(result["return"].WDC.sellorders[0]); 

我使用["return"]而不是。 .return因为一些旧的浏览器将跳过后者。

要迭代sellorders数组,您可以使用for语句或.forEach()