提问人:ML33M 提问时间:10/18/2023 最后编辑:jay.sfML33M 更新时间:10/19/2023 访问量:38
嵌套列表中的 flattern 子列表,用于整洁的 json 文件输出,没有不需要的数组括号
Flattern sublists within nested list for tidy json file output without unwanted array brackets
问:
因此,简而言之,我几乎没有弄清楚如何从csv文件生成一个巨大的嵌套列表,目的是以漂亮,整洁的json格式输出它。
但是,问题是我的 json 输出为每个子列表提供了一个“[]”作为 json 输出中的数据数组,导致括号过多“[]”,这有点搞砸了下一步过程。
我已经阅读了一些关于此的内容,并且有漂亮且auto_unbox只有几个括号。
jsonlite::toJSON(updated_nested_lists, pretty=TRUE,auto_unbox = TRUE)
所需的 JSON 应如下所示
我的 JSON 现在看起来像这样
我认为这意味着我应该在某些级别解压缩我的嵌套列表以实现此目的。但是我被困在如何将它们从里到外解压缩到特定列表上,当我使用 unlist 玩“订单”子列表时,它有点把所有东西都砸在一起,失去了最内部的数据结构。我想对完整的数据这样做(相同的结构,要大得多)
此处的最小可重复数据:
list(list(startTimestamp = structure(1620293100, class = c("POSIXct",
"POSIXt"), tzone = "UTC"), endTimestamp = structure(1620293998.66, class =
c("POSIXct",
"POSIXt"), tzone = "UTC"), orders = structure(list(structure(list(
timestamp = structure(c(1620293100.88, 1620293100.88, 1620293100.88,
1620293100.88, 1620293100.88), tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = c(22.63, 22.63, 22.63, 22.63, 22.63
), type = c("Mid", "Mid", "Mid", "Mid", "Mid"), volume = c(100L,
100L, 100L, 100L, 100L), tradeSum = c(2263, 2263, 2263, 2263,
2263)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-5L)), structure(list(timestamp = structure(1620293100.88, tzone = "UTC", class =
c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Mid", volume = 300L,
tradeSum = 6789), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293100.88, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Mid", volume = 1000L,
tradeSum = 22630), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293100.88, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Mid", volume = 3600L,
tradeSum = 81468), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293444.68, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.64, type = "Bid", volume = 200L,
tradeSum = 4528), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293446.74, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.64, type = "Ask", volume = 2700L,
tradeSum = 61128), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293453.78, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 600L,
tradeSum = 13578), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293455.55, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.64, type = "Ask", volume = 100L,
tradeSum = 2264), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293457.92, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.64, type = "Ask", volume = 200L,
tradeSum = 4528), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293468.28, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 500L,
tradeSum = 11315), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293470.52, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 300L,
tradeSum = 6789), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293482.13, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 700L,
tradeSum = 15841), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293482.13, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 900L,
tradeSum = 20367), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293487.69, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 200L,
tradeSum = 4526), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293501.04, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 100L,
tradeSum = 2262), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293506.57, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 400L,
tradeSum = 9052), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293531.71, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 200L,
tradeSum = 4524), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293578.02, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Bid", volume = 200L,
tradeSum = 4522), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293585.77, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Bid", volume = 100L,
tradeSum = 2261), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293588.74, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Bid", volume = 100L,
tradeSum = 2261), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293589.1, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Bid", volume = 100L,
tradeSum = 2261), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293589.1, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Bid", volume = 300L,
tradeSum = 6783), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293608.04, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Bid", volume = 100L, tradeSum = 2260), class =
c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
timestamp = structure(1620293633.1, tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Bid", volume = 200L,
tradeSum = 4520), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293639.58,
tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Bid", volume = 200L, tradeSum = 4520), class =
c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
timestamp = structure(1620293642.91, tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Ask", volume = 100L,
tradeSum = 2260), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293642.91, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Ask", volume = 2900L,
tradeSum = 65540), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293654.03, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Ask", volume = 300L, tradeSum = 6780), class =
c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
timestamp = structure(1620293656.97, tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 100L,
tradeSum = 2262), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293660, tzone =
"UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 700L,
tradeSum = 15834), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293663, tzone =
"UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 300L,
tradeSum = 6786), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293666.03, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 200L,
tradeSum = 4524), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293672.01, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 400L,
tradeSum = 9048), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(c(1620293674.65,
1620293674.65), tzone = "UTC", class = c("POSIXct", "POSIXt")),
tradePrice = c(22.62, 22.62), type = c("Ask", "Ask"), volume = c(200L,
200L), tradeSum = c(4524, 4524)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -2L)), structure(list(timestamp =
structure(1620293674.65, tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 3600L,
tradeSum = 81432), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293677.64, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 100L,
tradeSum = 2263), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293677.67, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 2200L,
tradeSum = 49786), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293677.92, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 800L,
tradeSum = 18104), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293686.34, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.6, type = "Bid", volume = 200L, tradeSum = 4520), class =
c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
timestamp = structure(1620293698.61, tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 100L,
tradeSum = 2262), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293707.6, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 400L,
tradeSum = 9048), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293723.01, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 400L,
tradeSum = 9052), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293723.26, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 300L,
tradeSum = 6789), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293723.29, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 100L,
tradeSum = 2262), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293723.29, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 100L,
tradeSum = 2262), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293723.29, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Ask", volume = 300L,
tradeSum = 6789), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293734.89, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 200L,
tradeSum = 4524), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293743.34, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 800L,
tradeSum = 18096), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293743.52, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Ask", volume = 1900L,
tradeSum = 42978), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(c(1620293743.95,
1620293743.95), tzone = "UTC", class = c("POSIXct", "POSIXt")),
tradePrice = c(22.62, 22.62), type = c("Ask", "Ask"), volume = c(1000L,
1000L), tradeSum = c(22620, 22620)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -2L)), structure(list(
timestamp = structure(c(1620293771.07, 1620293771.07), tzone = "UTC", class =
c("POSIXct",
"POSIXt")), tradePrice = c(22.61, 22.61), type = c("Bid",
"Bid"), volume = c(1400L, 1400L), tradeSum = c(31654, 31654
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-2L)), structure(list(timestamp = structure(1620293771.07, tzone = "UTC", class =
c("POSIXct",
"POSIXt")), tradePrice = 22.62, type = "Bid", volume = 400L,
tradeSum = 9048), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293772.39, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Ask", volume = 700L,
tradeSum = 15827), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293772.42, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.61, type = "Ask", volume = 900L,
tradeSum = 20349), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(c(1620293772.78,
1620293772.78), tzone = "UTC", class = c("POSIXct", "POSIXt")),
tradePrice = c(22.61, 22.61), type = c("Ask", "Ask"), volume = c(100L,
100L), tradeSum = c(2261, 2261)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -2L)), structure(list(timestamp =
structure(c(1620293780.41,
1620293780.41), tzone = "UTC", class = c("POSIXct", "POSIXt")),
tradePrice = c(22.62, 22.62), type = c("Ask", "Ask"), volume = c(500L,
500L), tradeSum = c(11310, 11310)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -2L)), structure(list(timestamp =
structure(1620293815.65, tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.64, type = "Ask", volume = 100L,
tradeSum = 2264), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293845.36, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 100L,
tradeSum = 2263), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293845.36, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.63, type = "Bid", volume = 700L,
tradeSum = 15841), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293892.3, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.73, type = "Bid", volume = 100L,
tradeSum = 2273), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(timestamp = structure(1620293998.66, tzone
= "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = 22.66, type = "Bid", volume = 300L,
tradeSum = 6798), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L))), ptype = structure(list(timestamp = structure(numeric(0),
tzone = "UTC", class = c("POSIXct",
"POSIXt")), tradePrice = numeric(0), type = character(0), volume = integer(0),
tradeSum = numeric(0)), class = c("tbl_df", "tbl", "data.frame"
), row.names = integer(0)), class = c("vctrs_list_of", "vctrs_vctr",
"list"))))
答:
1赞
jay.sf
10/18/2023
#1
你的列表确实嵌套得太深了,试试ing。 在这里可能会有所帮助。recursive=
unlist
rapply
> unlist(rapply(updated_nested_lists, unlist, how='l', recursive=FALSE), recursive=FALSE) |>
+ jsonlite::toJSON(pretty=TRUE, auto_unbox=TRUE)
{
"startTimestamp": "2021-05-06 09:25:00",
"endTimestamp": "2021-05-06 09:39:58",
"orders": [
{
"timestamp": ["2021-05-06 09:25:00", "2021-05-06 09:25:00", "2021-05-06 09:25:00", "2021-05-06 09:25:00", "2021-05-06 09:25:00"],
"tradePrice": [22.63, 22.63, 22.63, 22.63, 22.63],
"type": ["Mid", "Mid", "Mid", "Mid", "Mid"],
"volume": [100, 100, 100, 100, 100],
"tradeSum": [2263, 2263, 2263, 2263, 2263]
},
{
"timestamp": "2021-05-06 09:25:00",
"tradePrice": 22.63,
"type": "Mid",
"volume": 300,
"tradeSum": 6789
},
...
如果可能,请查看数据生成代码,看看是否可以避免此嵌套,而不是使用此修复程序。
请注意,您可以轻松检查str
> str(updated_nested_lists)
List of 1
$ :List of 3
..$ startTimestamp: POSIXct[1:1], format: "2021-05-06 09:25:00"
..$ endTimestamp : POSIXct[1:1], format: "2021-05-06 09:39:58"
..$ orders :List of 61
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5 obs. of 5 variables:
.. .. ..$ timestamp : POSIXct[1:5], format: "2021-05-06 09:25:00" "2021-05-06 09:25:00" "2021-05-06 09:25:00" ...
.. .. ..$ tradePrice: num [1:5] 22.6 22.6 22.6 22.6 22.6
.. .. ..$ type : chr [1:5] "Mid" "Mid" "Mid" "Mid" ...
.. .. ..$ volume : int [1:5] 100 100 100 100 100
.. .. ..$ tradeSum : num [1:5] 2263 2263 2263 2263 2263
.. ..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1 obs. of 5 variables:
.. .. ..$ timestamp : POSIXct[1:1], format: "2021-05-06 09:25:00"
.. .. ..$ tradePrice: num 22.6
评论
0赞
ML33M
10/19/2023
谢谢你的想法。请允许我澄清一下,当您说“这是怎么发生的......”时,您指的是在时间戳/交易价格 22.63 的第一次测试中添加的 []?我认为这就是数据的问题:拥有完全相同的项目。
0赞
jay.sf
10/19/2023
@ML33M 在应用修补程序之前,查看数据生成代码以评估嵌套的必要性。当然,只有当您不只是收到数据时才有可能。
0赞
ML33M
10/19/2023
谢谢你,我从软件方面得到了这个要求,他们希望以某种格式获得这些数据,因此我试图为 json 生成做疯狂的嵌套列表。我从另一家供应商那里得到的原始数据只是普通的 csv 文件。
1赞
jay.sf
10/19/2023
@ML33M 两者都去 ,它看起来很混乱,因为它是作为附加参数添加的(在 中),你也可以在那里使用匿名函数。recursive=F
unlist
rapply
...
rapply(updated_nested_lists, \(x) unlist(x, recursive=FALSE), how='l')
1赞
ML33M
10/22/2023
谢谢!很高兴我学到了新东西!
评论