2013-01-23 26 views
3

我使用R包rjson从Wunderground.com下载天气数据。我经常离开程序运行,没有问题,数据收集良好。然而,往往程序停止运行,并且我收到以下错误消息:fromJSON中的错误(paste(raw.data,collapse =“”)):未关闭的字符串

Error in fromJSON(paste(raw.data, collapse = "")) : unclosed string 
In addition: Warning message: 
In readLines(conn, n = -1L, ok = TRUE) : 
    incomplete final line found on 'http://api.wunderground.com/api/[my_API_code]/history_20121214pws:1/q/pws:IBIRMING7.json' 

有谁知道这意味着什么,并且由于它收集数据,因为我想我停止我的程序如何避免呢?

非常感谢,

+0

您确定该软件包名为'fromJSON'吗? 'rjson'和'RJSONIO'包都有这个名字的函数,但是我找不到一个叫做那个的包。 –

+0

是的 - 抱歉 - 你是对的。这是rjson。 – ben18785

回答

2

我可以用rjson包重新创建错误消息。

下面是一个有效的例子。

rjson::fromJSON('{"x":"a string"}') 
# $x 
# [1] "a string" 

如果我们忽略从x值双引号,那么我们得到的错误信息。

rjson::fromJSON('{"x":"a string}') 
# Error in rjson::fromJSON("{\"x\":\"a string}") : unclosed string 

RJSONIO的包的行为略有不同。它不会抛出错误,而是默默地返回一个NULL值。

RJSONIO::fromJSON('{"x":"a string}') 
# $x 
# NULL 
+0

太好了 - 我会试试rjsonio套餐!非常感谢 – ben18785

+0

请注意这一点。你仍然需要检查任何NULL值,并考虑如何处理它们。另一种方法是使用'rjson'并在'try'或'tryCatch'内将调用封装到'fromJSON'中。 –