尝试将json解析为R时出错 [英] Error while trying to parse json into R

查看:209
本文介绍了尝试将json解析为R时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用R,并且有一个有关在R中解析json以获取非json格式的任务.为此,我正在使用"fromJSON()" 函数.我试图将json解析为文本文件.当我仅使用一个行条目就可以成功运行.但是,当我尝试使用多个行条目时,出现以下错误:

I have recently started using R and have a task regarding parsing json in R to get a non-json format. For this, i am using the "fromJSON()" function. I have tried to parse json as a text file. It runs successfully when i do it with just a single row entry. But when I try it with multiple row entries, i get the following error:

 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文本时使用的三种不同格式,但结果是相同的,只是更改了建议的位置. 请帮助我确定此错误的原因,或者是否有更有效的方法来执行任务.

The above errors are due to three different formats in which i tried to parse the json text, but the result was the same, only the location suggested by changed. Please help me to identify the cause of this error or if there is a more efficient way o performing the task.

我拥有的原始文件是具有多个列的excel工作表,这些列之一由json文本组成.我现在尝试的方法是仅提取json列,并将其转换为制表符分隔的文本,然后将其解析为:

The original file that i have is an excel sheet with multiple columns and one of those columns consists of json text. The way i tried right now is by extracting just the json column and converting it to a tab separated text and then parsing it as:

fromJSON("D:/Eclairs/Printing/test3.txt")

请还建议是否可以更有效地完成此操作.我还需要将excel中的所有列都映射到非json文本.

Please also suggest if this can be done more efficiently. I need to map all the columns in the excel to the non-json text as well.

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数据帧迭代保存到不断增长的列表中:

Consider reading in the text line by line with readLines(), iteratively saving the JSON dataframes to a growing list:

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

这篇关于尝试将json解析为R时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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