【问题标题】:R - Trying to parse a json and convert in to to a data frameR - 尝试解析 json 并转换为数据框
【发布时间】:2020-10-26 13:58:00
【问题描述】:

我正在尝试但无法理解如何转换以下 json:

["{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"timestamp\",\"type\":\"datetime\"},{\"name\":\"count_\",\"type\":\"long\"}],\"rows\":[[\"2020-10-26T00:00:00Z\",13937],[\"2020-10-25T00:00:00Z\",94029]]}]}"] 

进入像这样的数据框:

【问题讨论】:

  • 你在stackoverflow中搜索过吗?请参阅此链接。使用 jsonlite 包可能对您有用:stackoverflow.com/questions/36454638/…
  • 您是否检查过您的json 字符串。在firefox 上解析时显示无效数据。请检查。

标签: r json dataframe


【解决方案1】:

使用jsonlite::fromJSON

json <- "{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"timestamp\",\"type\":\"datetime\"},{\"name\":\"count_\",\"type\":\"long\"}],\"rows\":[[\"2020-10-26T00:00:00Z\",13937],[\"2020-10-25T00:00:00Z\",94029]]}]}"
dat <- jsonlite::fromJSON(json)
res <- setNames(as.data.frame(dat$tables$rows[[1]]), unlist(dat$tables$columns[[1]][1, ]))
res
#              timestamp datetime
# 1 2020-10-26T00:00:00Z    13937
# 2 2020-10-25T00:00:00Z    94029

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2014-08-22
    • 2016-05-09
    • 2016-09-29
    • 2021-01-28
    • 2015-12-30
    相关资源
    最近更新 更多