2

I'm a bit new to this, so don't be too hard on me despite the fact that I probably sound like a noob.

I have a JSON source that I'm pulling from with the following JS code:

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

That gives me an output that looks like the following:

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

Which I set to a var "result".

Because there are multiple "price" values, I don't know how to just use the first one. Any ideas on how I could do this?

I'm using Node.js and jQuery just for reference.

4

1 回答 1

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().

于 2013-11-14T01:40:54.347 回答