2017-01-10 58 views
0

時出現無效的下標「list」錯誤我按照以下問題中提到的說明將JSON數據轉換爲使用RJSONIO包的數據幀。鏈接如下:從JSON轉換爲Dataframe時使用R

How to convert JSON to Dataframe

下面是我的數據的JSON總之,每個字段包含相等數量的值,大約五。顏色字段中的值是類型列表,我的猜測是這是導致問題的原因。

json 
title: chr 
remaining: chr 
color: list() 
brand: chr 
modelnum: chr 
size: chr 

我附上JSON值的樣本集,如果對社會任何人都可以闡明如何將這些模型轉換成數據幀的一些光,這將是偉大的!

樣品JSON數據:

{"title":"oneplus 3","remaining":"","color":[],"brand":"OnePlus","modelnum":"OnePlus 3","size":""} 
{"title":"oneplus 3 (soft gold, 64 gb)","remaining":"(soft)","color":["gold"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64 gb"} 
{"title":"deal 1:oneplus 3 (graphite, 64gb) 6gb ram 4g lte - 1 year manufacture warranty","remaining":"deal 1: 6gb ram 4g lte - 1 year manufacture warranty","color":["graphite"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64gb"} 
{"title":"oneplus 3 (graphite, 64 gb)","remaining":"","color":["graphite"],"brand":"OnePlus","modelnum":"OnePlus 3","size":"64 gb"} 
{"title":"xiaomi redmi note 3 32gb","remaining":"","color":[],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"} 
{"title":"xiaomi redmi note 3 (grey 32 gb) mobile phone","remaining":"mobile phone","color":["grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32 gb"} 
{"title":"xiaomi redmi note 3 new (6 month brand warranty)","remaining":"new (6 month brand warranty)","color":[],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":""} 
{"title":"xiaomi redmi note 3 (gold 32gb) mobile phone","remaining":"mobile phone","color":["gold"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"} 
{"title":"xiaomi redmi note 3 (dark grey) (32gb)","remaining":"","color":["dark grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"} 
{"title":"mi redmi note 3 32gb dark grey","remaining":"mi","color":["dark grey"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"} 
{"title":"xiaomi redmi note 3 (gold, 32gb)","remaining":"","color":["gold"],"brand":"Xiaomi","modelnum":"Redmi Note 3","size":"32gb"} 

R-代碼:

library(RJSONIO) 
json <- fromJSON(file_path_for_the_above_data, nullValue = NA) 
dat <- lapply(json, function(j) { 
as.data.frame(replace(j, sapply(j, is.list), NA)) 
}) 

這是錯誤發生。

錯誤更換(J,sapply(J,is.list),NA): 無效標類型 '列表'

謝謝。

回答

0

問題在於JSON格式錯誤,修復JSON數組基本上做到了這一點。