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

时间:2013-11-14 01:33:59

标签: javascript json node.js

我对此有点新意,所以尽管我听起来像个菜鸟,但不要太难我。

我有一个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“result”。

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

我正在使用Node.js和jQuery作为参考。

1 个答案:

答案 0 :(得分:3)

您的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是为您自动解析的,所以您需要做的就是像普通对象一样使用它。

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

我使用了["return"]而不是.return,因为一些旧的浏览器会绊倒后者。

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

相关问题