尝试将json解析为R时出错

时间:2016-11-06 10:40:32

标签: json r text jsonlite

我最近开始使用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工作表,其中一列包含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"}]

1 个答案:

答案 0 :(得分:1)

考虑在文本中逐行读取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