读取R中的json文件:词法错误:json文本中的无效char [英] reading a json file in R: lexical error: invalid char in json text

查看:692
本文介绍了读取R中的json文件:词法错误:json文本中的无效char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我使用的代码示例:

Here is an example of the code I'm using:

library(jsonlite)
library(curl)

#url
url = "http://www.zillow.com/search/GetResults.htm?spt=homes&status=001000&lt=000000&ht=010000&pr=999999,10000001&mp=3779,37788&bd=0%2C&ba=0%2C&sf=,&lot=0%2C&yr=,1800&singlestory=0&hoa=0%2C&pho=0&pets=0&parking=0&laundry=0&income-restricted=0&pnd=0&red=0&zso=0&days=36m&ds=all&pmf=0&pf=0&sch=100111&zoom=6&rect=-91307373,29367814,-84759521,35554574&p=1&sort=globalrelevanceex&search=maplist&rid=4&rt=2&listright=true&isMapSearch=true&zoom=6"

#json
results_data_json = fromJSON(txt = url)

我曾经能够运行类似的代码而没有任何问题.现在出现以下错误:

I used to be able to run similar code to this with no issue. Now I'm getting the following error:

Error in feed_push_parser(buf) : 
  lexical error: invalid char in json text.
                                   <html><head><title>Zillow: Real
                 (right here) ------^

有什么想法吗?

推荐答案

这是我从文件中读取json时发生的.该代码有一天有效,然后第二天出现此错误.尽管我不明白为什么我的解决方案有效,但我最终还是能够解决该错误.首先,我发现了一个github帖子,建议添加readLines()函数.例如.

This happened to me reading in a json from a file. The code worked one day, and then the next day I got this error. I was eventually able to circumvent the error although I do not understand why my solution works. First, I found a github post that suggested adding the readLines() function. Eg.

r_object <- fromJSON(readLines("file.json"))

当我这样做时,json正确加载了,但出现了以下警告:

When I did this the json loaded correctly but I got the following warning:

Warning message:
In readLines("file.json") : incomplete final line found on 'file.json'.

然后,出于特殊原因,我尝试在JSON底部添加另一行.最后一个花括号后的空白行.这样就解决了.不知道为什么.如果有人知道为什么这样做,请发表评论.

Then, for no particular reason, I tried adding an additional line to the bottom of the JSON. Just a blank line after the last curly bracket. And that fixed it. No idea why. If anyone knows why this worked, please drop a comment.

这篇关于读取R中的json文件:词法错误:json文本中的无效char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆