1

我有一个 json 格式的时间序列数据,我想从 json 格式中提取数据并将其转换为 R 中的数据框。我已经安装了 rjson 和 jsonlite 包并加载了库。但是,我在运行“fromjson”命令时遇到了以下错误。有谁知道如何解决它?

谢谢

代码

   library("rjson", lib.loc="/Users/r_beginer/Library/R/3.0/library")
   library("rjsonlite", lib.loc="/Users/r_beginer/Library/R/3.0/library")
   test_data=fromJSON(file='/Users/r_beginer/Desktop/logins.json')
   Error in is.character(txt) : 'txt' is missing

json格式数据

  ["2014-03-01 00:01:54", "2014-03-01 00:04:52", "2014-03-01 00:06:03", 
   "2014-03-01 00:12:11", "2014-03-01 00:14:54", "2014-03-01 00:16:23", "2014-03-01 00:17:19"]
4

1 回答 1

0

首先,我认为您可能希望调用library(jsonlite)not rjsonlite

您的示例不是很可复制,但以下内容对我有用,尽管正如 B.Mr.W 指出的那样,JSON 中应该有这些值的名称。

library(rjson)

no_name <- '["2014-03-01 00:01:54", "2014-03-01 00:04:52", "2014-03-01 00:06:03", 
   "2014-03-01 00:12:11", "2014-03-01 00:14:54", "2014-03-01 00:16:23", "2014-03-01 00:17:19"]'

write(no_name,"temp.json")

fromJSON(file="temp.json") # returns the correct character array

some_json <- '{"timeStamp":["2014-03-01 00:01:54", "2014-03-01 00:04:52", "2014-03-01 00:06:03", 
   "2014-03-01 00:12:11", "2014-03-01 00:14:54", "2014-03-01 00:16:23", "2014-03-01 00:17:19"]}'

fromJSON(some_json) # returns a list with one element, $timeStamp, a character array
于 2014-10-15T17:51:31.647 回答