我有一个双精度浮点数向量
当我使用第二个任意向量通过 RJSONIO 解析器运行
我尝试了几种方法来简化在调用
有没有办法简化调用
x
,其中每个元素表示一个POSIX日期时间。x <- c(1417621083, 1417621204, 1417621384, 1417621564, 1417621623)
我正在使用RJSONIO包,并希望继续使用。
作为练习,我想将这些值转换为JSON文本,然后再次读取到R中,但是在获取日期时间表示并将其转换为一个简单的列表结果方面遇到了麻烦。 在JSON中,日期需要采用特殊格式,因此x中的值被转换为以下内容:
dates <- c("/new Date(1417621083)", "/Date(1417621204)", "/Date(1417621384)",
"/Date(1417621564)", "/Date(1417621623)")
当我使用第二个任意向量通过 RJSONIO 解析器运行
dates
时,一切似乎都很顺利。library(RJSONIO)
make <- toJSON(list(date = dates, value = LETTERS))
然后,当我使用R-json C例程中的stringFun
选项来解析新的JSON文本时,结果是一个两个元素的列表,第一个元素是一个列表,第二个元素是一个原子向量。
(read <- fromJSON(make, stringFun = "R_json_dateStringOp"))
# $date
# $date[[1]]
# [1] "2014-12-03 07:38:03 PST"
#
# $date[[2]]
# [1] "2014-12-03 07:40:04 PST"
#
# $date[[3]]
# [1] "2014-12-03 07:43:04 PST"
#
# $date[[4]]
# [1] "2014-12-03 07:46:04 PST"
#
# $date[[5]]
# [1] "2014-12-03 07:47:03 PST"
#
#
# $value
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
# [14] "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
但我希望得到两个向量的列表,并且更喜欢它以以下形式呈现:
# $date
# [1] "2014-12-03 07:38:03 PST" "2014-12-03 07:40:04 PST"
# [3] "2014-12-03 07:43:04 PST" "2014-12-03 07:46:04 PST"
# [5] "2014-12-03 07:47:03 PST"
#
# $value
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
# [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
我尝试了几种方法来简化在调用
fromJSON()
内部的结果,但都没有成功。以下是我的几次尝试:
使用处理程序: 这会简化结果,但无法重新格式化日期。h1 <- basicJSONHandler(simplify = TRUE)
fromJSON(make, handler = h1, stringFun = "R_json_dateStringOp")
# $date
# [1] "/new Date(1417621083)" "/Date(1417621204)"
# [3] "/Date(1417621384)" "/Date(1417621564)"
# [5] "/Date(1417621623)"
#
# $value
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
# [14] "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
尝试简化参数: 我尝试了几种不同的方法,但都没有成功。
fromJSON(make, simplify = StrictCharacter)
# $date
# [1] "/new Date(1417621083)" "/Date(1417621204)"
# [3] "/Date(1417621384)" "/Date(1417621564)"
# [5] "/Date(1417621623)"
#
# $value
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
# [14] "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
有没有办法简化调用
fromJSON()
的日期结果?