R语言转换Binance API JSON为DataFrame时遇行数不匹配错误
Hi everyone,
I'm a total forum newbie—only been working with R, JSON data, and APIs for 3 days now, so please forgive me if I don't explain things perfectly! I'm really excited to dive into learning all this though.
I’ve been trying to turn the JSON data from the Binance ETHBTC order book depth endpoint into a usable DataFrame in R, but I keep hitting an error. Here’s the simplified code I’m using:
library(jsonlite) library(httr) endpoint <- "https://api.binance.com/api/v1/depth?symbol=ETHBTC" dt_text <- content(GET(endpoint), "text") dt_json <- fromJSON(dt_text, flatten = TRUE) df_GetDepth <- as.data.frame(dt_json)
When I run this, I get the following error:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 0
After debugging, I can see the error pops up on the final line where I convert the JSON object to a data frame. I think the problem is that the first field in the JSON (lastUpdateId) only has 1 value, while the bids and asks fields contain nested arrays with multiple rows. Here’s a snippet of the JSON response to show the structure:
{ "lastUpdateId": 1027024, "bids": [ [ "4.00000000", // PRICE "431.00000000", // QTY [] // Can be ignored ] ], "asks": [ [ "4.00000200", "12.00000000", [] ] ] }
I’d really appreciate any help figuring out how to fix this so I can get the data into a proper, usable DataFrame. Thanks so much in advance!
内容的提问来源于stack exchange,提问作者Adityendra Suman

