尝试将 json 解析为 R 时出错
Error while trying to parse json into R
我最近开始使用 R,并且有一项任务是在 R 中解析 json 以获得非 json 格式。为此,我正在使用 "fromJSON()" 函数。我试图将 json 解析为文本文件。当我只使用一个行条目时,它运行成功。但是当我尝试使用多行条目时,出现以下错误:
fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
lexical error: invalid char in json text.
[{'CategoryType':'dining','City':
(right here) ------^
> fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
parse error: trailing garbage
"mumbai","Location":"all"}] [{"JourneyType":"Return","Origi
(right here) ------^
> fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
parse error: after array element, I expect ',' or ']'
:"mumbai","Location":"all"} {"JourneyType":"Return","Origin
(right here) ------^
上面的错误是由于我试图解析json文本的三种不同格式,但结果是一样的,只是建议的位置发生了变化。
请帮助我确定此错误的原因,或者是否有更有效的方法来执行该任务。
我拥有的原始文件是一个包含多列的 excel sheet,其中一列包含 json 文本。我现在尝试的方法是仅提取 json 列并将其转换为制表符分隔的文本,然后将其解析为:
fromJSON("D:/Eclairs/Printing/test3.txt")
还请建议是否可以更有效地完成此操作。我还需要将 excel 中的所有列映射到非 json 文本。
Example:
[{"CategoryType":"dining","City":"mumbai","Location":"all"}]
[{"CategoryType":"reserve-a-table","City":"pune","Location":"Kothrud,West Pune"}]
[{"Destination":"Mumbai","CheckInDate":"14-Oct-2016","CheckOutDate":"15-Oct-2016","Rooms":"1","NoOfPax":"3","NoOfAdult":"3","NoOfChildren":"0"}]
考虑使用 readLines()
逐行阅读文本,将 JSON 数据帧迭代保存到不断增长的列表中:
library(jsonlite)
con <- file("C:/Path/To/Jsons.txt", open="r")
jsonlist <- list()
while (length(line <- readLines(con, n=1, warn = FALSE)) > 0) {
jsonlist <- append(jsonlist, list(fromJSON(line)))
}
close(con)
jsonlist
# [[1]]
# CategoryType City Location
# 1 dining mumbai all
# [[2]]
# CategoryType City Location
# 1 reserve-a-table pune Kothrud,West Pune
# [[3]]
# Destination CheckInDate CheckOutDate Rooms NoOfPax NoOfAdult NoOfChildren
# 1 Mumbai 14-Oct-2016 15-Oct-2016 1 3 3 0
我最近开始使用 R,并且有一项任务是在 R 中解析 json 以获得非 json 格式。为此,我正在使用 "fromJSON()" 函数。我试图将 json 解析为文本文件。当我只使用一个行条目时,它运行成功。但是当我尝试使用多行条目时,出现以下错误:
fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
lexical error: invalid char in json text.
[{'CategoryType':'dining','City':
(right here) ------^
> fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
parse error: trailing garbage
"mumbai","Location":"all"}] [{"JourneyType":"Return","Origi
(right here) ------^
> fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
parse error: after array element, I expect ',' or ']'
:"mumbai","Location":"all"} {"JourneyType":"Return","Origin
(right here) ------^
上面的错误是由于我试图解析json文本的三种不同格式,但结果是一样的,只是建议的位置发生了变化。 请帮助我确定此错误的原因,或者是否有更有效的方法来执行该任务。
我拥有的原始文件是一个包含多列的 excel sheet,其中一列包含 json 文本。我现在尝试的方法是仅提取 json 列并将其转换为制表符分隔的文本,然后将其解析为:
fromJSON("D:/Eclairs/Printing/test3.txt")
还请建议是否可以更有效地完成此操作。我还需要将 excel 中的所有列映射到非 json 文本。
Example:
[{"CategoryType":"dining","City":"mumbai","Location":"all"}]
[{"CategoryType":"reserve-a-table","City":"pune","Location":"Kothrud,West Pune"}]
[{"Destination":"Mumbai","CheckInDate":"14-Oct-2016","CheckOutDate":"15-Oct-2016","Rooms":"1","NoOfPax":"3","NoOfAdult":"3","NoOfChildren":"0"}]
考虑使用 readLines()
逐行阅读文本,将 JSON 数据帧迭代保存到不断增长的列表中:
library(jsonlite)
con <- file("C:/Path/To/Jsons.txt", open="r")
jsonlist <- list()
while (length(line <- readLines(con, n=1, warn = FALSE)) > 0) {
jsonlist <- append(jsonlist, list(fromJSON(line)))
}
close(con)
jsonlist
# [[1]]
# CategoryType City Location
# 1 dining mumbai all
# [[2]]
# CategoryType City Location
# 1 reserve-a-table pune Kothrud,West Pune
# [[3]]
# Destination CheckInDate CheckOutDate Rooms NoOfPax NoOfAdult NoOfChildren
# 1 Mumbai 14-Oct-2016 15-Oct-2016 1 3 3 0