2016-12-05 62 views
0

我想这个JSON导入所需的数据R的URL数据帧R数据帧是http://mooshe.pw/files/items_rs3.json获取JSON数据转换成具有怪异的格式

这里是我的代码:

library(jsonlite) 

item.codes <- fromJSON('http://mooshe.pw/files/items_rs3.json') 

但是,试图运行这个时,我得到了以下错误消息:

Error in feed_push_parser(buf) : 
    parse error: after key and value, inside map, I expect ',' or '}' 
      "name": "Key block labelled "2"",  "members": true,  "t 
       (right here) ------^ 

因此,在一些数据的值中有引号,这意味着R有一个很难解析JSO N并将其作为数据框。我怎样才能解决这个问题?

+0

它是无效的JSON。 R不是“有一段艰难的时间”,它正确地标记一个无效文件。 – hrbrmstr

回答

2

RJSONIO::fromJSON是多一点宽容不规则的语法,并且会给你一个列表,你可以组装成一个列表与purrr

library(purrr) 

l <- RJSONIO::fromJSON('http://mooshe.pw/files/items_rs3.json') 

df <- l %>% transpose() %>% map_df(simplify) 

df 
## # A tibble: 23,972 × 6 
##     name members tradeable cosmetic modelid value 
##     <chr> <lgl>  <lgl> <lgl> <dbl> <dbl> 
## 1  Dwarf remains TRUE  FALSE FALSE 2595  -1 
## 2    Toolkit TRUE  FALSE FALSE 2679  -1 
## 3   Cannonball TRUE  TRUE FALSE 2413  5 
## 4 Nulodion's notes TRUE  FALSE FALSE 2794  -1 
## 5   Ammo mould TRUE  FALSE FALSE 2706  5 
## 6 Instruction manual TRUE  FALSE FALSE 2794  10 
## 7   Cannon base TRUE  TRUE FALSE 63866 187500 
## 8  Cannon stand TRUE  TRUE FALSE 63876 187500 
## 9  Cannon barrels TRUE  TRUE FALSE 63868 187500 
## 10  Cannon furnace TRUE  TRUE FALSE 63870 187500 
## # ... with 23,962 more rows 
+0

这正是我想要做的!谢谢! :) – Kyle

2

再一次,它是无效的JSON,你需要修复。这是做的一个方法:

library(magrittr) 
library(stringi) 
library(jsonlite) 

readLines("http://mooshe.pw/files/items_rs3.json") %>% 
    stri_replace_all_regex('(")([[:alnum:][:blank:]]+)("")', "'$2'\"") %>% 
    fromJSON()