使用RJSONIO :: fromJSON()简化POSIX节点 [英] Simplifying a POSIX node with RJSONIO::fromJSON()
问题描述
我有以下双精度值向量x
,其中每个元素代表POSIX日期时间
I have the following vector of double values, x
, where each element represents a POSIX date-time
x <- c(1417621083, 1417621204, 1417621384, 1417621564, 1417621623)
我正在使用 RJSONIO 包,并希望继续这样做.
I am using the RJSONIO package, and would like to continue to do so.
作为练习,我想将这些值转换为JSON文本,然后再次将它们读回R,但是在将日期时间表示形式转换为漂亮的简化列表结果时遇到了麻烦.在JSON中,日期必须采用特殊格式,因此x
中的值将转换为以下格式:
As an exercise, I'd like to convert these values into JSON text and then read them back into R again, but am having trouble getting the date-time representations into a nice simplified list result. In JSON, the dates need to be in a special format so the values in x
are converted to the following:
dates <- c("/new Date(1417621083)", "/Date(1417621204)", "/Date(1417621384)",
"/Date(1417621564)", "/Date(1417621623)")
当我通过 RJSONIO 解析器使用第二个任意向量运行dates
时,一切似乎都顺利进行.
When I run dates
with a second arbitrary vector through the RJSONIO parser, everything seems to go smoothly.
library(RJSONIO)
make <- toJSON(list(date = dates, value = LETTERS))
然后,当我使用stringFun
选项和R-json C例程解析日期的新JSON文本时,结果是一个包含两个元素的列表,第一个元素是一个列表,第二个元素是一个原子向量.
Then when I parse the new JSON text using the stringFun
option with the R-json C routine for dates, the result is a two-element list, the first element being a list and the second an atomic vector.
(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"
但是我期望有两个向量的列表,而我宁愿以
But I was expecting a list of two vectors, and I would rather have it in the form of
# $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()
的调用中的结果,但没有一种有效.这是我的一些尝试:
I tried several ways to simplify the result from within the call to fromJSON()
, and none of them have worked. Here are a couple of my attempts:
使用处理程序::这简化了结果,但无法重新设置日期
Using a handler : This simplifies the result but fails to reformat the dates
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"
尝试 simplify
参数:我尝试了几种不同的方法,但均无济于事.
Trying the simplify
argument : I tried several different varieties of this and none worked.
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()
的调用中日期的结果?
Is there a way to simplify the result for the dates in the call to fromJSON()
?
推荐答案
我认为您无法同时获得日期的强制和它们对矢量的简化.出于简单原因,尚未在RJSONIO
中实现此功能.确实,正如您提到的,简化是使用标志StrictLogical
,StrictNumeric
和StrictCharacter
中的一个完成的,该标志创建逻辑,数字或字符向量.也许您应该与维护人员联系以为POSIXct
日期添加StrictPosixct
标志.
I think you cannot get the coercion of dates and their simplification to a vector in the same time. For the simple reason that this is not (yet) implemented in RJSONIO
. Indeed as you mention the simplification is done using one the flag : StrictLogical
, StrictNumeric
and StrictCharacter
which create logicals, numbers or characters vectors. Maybe you should contact the maintainer to add StrictPosixct
flag for POSIXct
dates.
使用stringFun
无济于事,因为它接收标量元素(字符串)并且不知道其他矢量元素.您可以通过将R函数定义为stringFun参数并在其中放置浏览器来进行检查.
Using stringFun
can't help because it receives a scalar element(a character string) and it is not aware of other vector elements. You can check this by defining an R function as stringFun parameter and put a browser within it.
convertJSONDate <-
function(x)
{
if(grepl('Date',x)){
val <- sub('.*[(]([0-9]+).*','\\1',x)
return(structure(as.numeric(val)/1000, class = c("POSIXct", "POSIXt")))
}
x
}
我想出于性能原因,当您解析json时,您想进行强制/简化.我会使用其他策略:
I guess you want to do the coercion/simplification when you parse your json for a performance reason. I would use a different strategy :
- 我将数字值强制转换为POSIXct,然后将它们存储为格式正确的日期中的字符.这比特殊的(丑陋的)"new Date(..,date)RJSONIO日期格式要好.请记住,json格式是可以由其他语言(python,js等)解析的标准格式.
- 然后将日期解析为正常字符,然后使用快速的
fasttime
软件包将其强制转换为POSIXct向量.
- I coerce my numeric values to a POSIXct and I will store them as a character in a well formatted dates. This is better then the special ( ugly) "new Date(.. ,date") RJSONIO date format. Remember that json format is a standard format that can be parsed by other languages ( python, js,..)
- Then parse my dates as a normal character and I use the fast
fasttime
package to coerce it to POSIXct vector.
此处显示了一些代码:
## coerce x to dates a well formatted dates
dd <- as.character(as.POSIXct(x,origin = '1970-01-01' , tz = "UTC"))
## read it again in a fast way
fastPOSIXct(fromJSON(make)$date)
[1] "2014-12-03 16:38:03 CET" "2014-12-03 16:40:04 CET" "2014-12-03 16:43:04 CET" "2014-12-03 16:46:04 CET" "2014-12-03 16:47:03 CET"
这篇关于使用RJSONIO :: fromJSON()简化POSIX节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!