[英]JSON returns multiple values with the same name
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: 我有一个使用以下JS代码提取的JSON源:
$.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". 我将其设置为var“结果”。
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. 我正在使用Node.js和jQuery仅供参考。
Your JSON structure actually looks like this: 您的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 ...
]
}
}
}
Because the JSON is parsed automatically for you, all you need to do is work with it like normal objects. 由于JSON是为您自动解析的,因此您所需要做的就是像处理普通对象一样使用它。
console.log(result["return"].WDC.sellorders[0]);
I used ["return"]
instead of .return
because some older browsers will trip over the latter. 我使用
["return"]
而不是.return
因为某些较旧的浏览器将在后者上跳闸。
To iterate the sellorders
Array, you'd use a for
statement, or .forEach()
. 要迭代
sellorders
数组,可以使用for
语句或.forEach()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.