【发布时间】:2022-09-28 05:13:09
【问题描述】:
我根本不是 Json 文件的专家,我正在努力完成一项简单的任务。 考虑下面存储在 test.json 中的 json 文件
{
\"entry_1\": {
\"AT\": null,
\"BE\": null,
\"BG\": null,
\"CY\": null,
\"CZ\": null,
\"DE\": null,
\"DK\": null,
\"EE\": null,
\"EL\": null,
\"ES\": null,
\"FI\": null,
\"FR\": null,
\"HR\": null,
\"HU\": null,
\"IE\": null,
\"IT\": null,
\"LT\": null,
\"LU\": null,
\"LV\": null,
\"MT\": null,
\"NL\": null,
\"PL\": null,
\"PT\": null,
\"RO\": null,
\"SI\": null,
\"SK\": null
},
\"entry_2\": {
\"AT\": null,
\"BE\": null,
\"BG\": null,
\"CY\": null,
\"CZ\": null,
\"DE\": null,
\"DK\": null,
\"EE\": null,
\"EL\": null,
\"ES\": null,
\"FI\": null,
\"FR\": null,
\"HR\": null,
\"HU\": null,
\"IE\": null,
\"IT\": null,
\"LT\": null,
\"LU\": null,
\"LV\": null,
\"MT\": null,
\"NL\": null,
\"PL\": null,
\"PT\": null,
\"RO\": null,
\"SI\": null,
\"SK\": null
},
\"entry_3\": {
\"AT\": null,
\"BE\": null,
\"BG\": null,
\"CY\": null,
\"CZ\": null,
\"DE\": null,
\"DK\": null,
\"EE\": null,
\"EL\": null,
\"ES\": null,
\"FI\": null,
\"FR\": null,
\"HR\": null,
\"HU\": null,
\"IE\": null,
\"IT\": null,
\"LT\": null,
\"LU\": null,
\"LV\": null,
\"MT\": null,
\"NL\": null,
\"PL\": null,
\"PT\": null,
\"RO\": null,
\"SI\": null,
\"SK\": null
},
\"entry_4\": {
\"AT\": null,
\"BE\": null,
\"BG\": null,
\"CY\": null,
\"CZ\": null,
\"DE\": null,
\"DK\": null,
\"EE\": null,
\"EL\": null,
\"ES\": null,
\"FI\": null,
\"FR\": null,
\"HR\": null,
\"HU\": null,
\"IE\": null,
\"IT\": null,
\"LT\": null,
\"LU\": null,
\"LV\": null,
\"MT\": null,
\"NL\": null,
\"PL\": null,
\"PT\": null,
\"RO\": null,
\"SI\": null,
\"SK\": null
},
\"entry_5\": {
\"AT\": null,
\"BE\": null,
\"BG\": null,
\"CY\": null,
\"CZ\": null,
\"DE\": null,
\"DK\": null,
\"EE\": null,
\"EL\": null,
\"ES\": null,
\"FI\": null,
\"FR\": null,
\"HR\": null,
\"HU\": null,
\"IE\": null,
\"IT\": null,
\"LT\": null,
\"LU\": null,
\"LV\": null,
\"MT\": null,
\"NL\": null,
\"PL\": null,
\"PT\": null,
\"RO\": null,
\"SI\": null,
\"SK\": null
}
}
我想在我的 R 会话中阅读它,然后修改它的条目并将其另存为新的 Json 文件。 根据我收到的建议,我在下面粘贴了一个修改后的代表
library(tidyverse)
library(jsonlite)
#>
#> Attaching package: \'jsonlite\'
#> The following object is masked from \'package:purrr\':
#>
#> flatten
entry_1 <- tibble(x=c(\"AT\", \"IT\", \"HU\"), value=c(12, 0.9, 4)) |>
pivot_wider(names_from= x, values_from=value)
entry_2 <- tibble(x=c(\"FR\", \"IE\", \"RO\"), value=c(1.2, 0.9, 4.8))|>
pivot_wider(names_from= x, values_from=value)
entry_3 <- tibble(x=c(\"DE\", \"FI\", \"EL\"), value=c(1.7, 0.09, 4.7))|>
pivot_wider(names_from= x, values_from=value)
entry_4 <- tibble(x=c(\"SK\", \"LT\", \"BG\"), value=c(1.8, 0.967, 4.6))|>
pivot_wider(names_from= x, values_from=value)
entry_5 <- tibble(x=c(\"FR\", \"IT\", \"IE\"), value=c(129, 9.4, 4.3))|>
pivot_wider(names_from= x, values_from=value)
newdata <- list(entry_1, entry_2, entry_3, entry_4, entry_5)
names(newdata) <- c(\"entry_1\", \"entry_2\", \"entry_3\", \"entry_4\", \"entry_5\")
newdataJSON<-toJSON(newdata, pretty=TRUE, auto_unbox = TRUE)
newdataJSON
#> {
#> \"entry_1\": [
#> {
#> \"AT\": 12,
#> \"IT\": 0.9,
#> \"HU\": 4
#> }
#> ],
#> \"entry_2\": [
#> {
#> \"FR\": 1.2,
#> \"IE\": 0.9,
#> \"RO\": 4.8
#> }
#> ],
#> \"entry_3\": [
#> {
#> \"DE\": 1.7,
#> \"FI\": 0.09,
#> \"EL\": 4.7
#> }
#> ],
#> \"entry_4\": [
#> {
#> \"SK\": 1.8,
#> \"LT\": 0.967,
#> \"BG\": 4.6
#> }
#> ],
#> \"entry_5\": [
#> {
#> \"FR\": 129,
#> \"IT\": 9.4,
#> \"IE\": 4.3
#> }
#> ]
#> }
由reprex package (v2.0.1) 于 2022 年 9 月 27 日创建
这非常接近我的需要。一件事只困扰我:有没有办法摆脱最终 Json 文件中的所有方括号?
-
你从旧数据框中保留了什么?你的预期输出是什么?听起来你只是想替换,那么为什么不制作一个新的 JSON 呢?
-
我和 dcsuka 在一起:如果你想代替新数字的原始值并保持其余字段不变,然后我可以看到想要就地修改源数据并将其写回文件。但是,既然您想丢弃未更改的字段,为什么不按照您需要的方式(作为命名列表,而不是 tibble)形成本地
entry_1并直接写入? -
非常感谢您的建议!从头开始构造一个新的 Json 实际上更简单。你知道如何去掉我在上面生成的更新的新 Json 文件中的方括号(我更新了 reprex)吗?