2015-07-20 53 views
0

我有有一個稍微不同格式的JSON文件:JSON解析成R數據幀

{"review/profileName": "Carleen M. Amadio \"Lady Dragonfly\"", "product/price": "unknown", "review/time": "1314057600", "product/productId": "B000GKXY4S", "review/helpfulness": "2/2", "review/summary": "Fun for adults too!", "review/userId": "A1QA985ULVCQOB", "product/title": "Crazy Shape Scissor Set", "review/score": "5.0", "review/text": "I really enjoy these scissors for my inspiration books that I am making (like collage, but in books) and using these different textures these give is just wonderful, makes a great statement with the pictures and sayings. Want more, perfect for any need you have even for gifts as well. Pretty cool!"}{"review/profileName": "Barbara", "product/price": "unknown", "review/time": "1328659200", "product/productId": "B000GKXY4S", "review/helpfulness": "0/0", "review/summary": "Making the cut!", "review/userId": "ALCX2ELNHLQA7", "product/title": "Crazy Shape Scissor Set", "review/score": "5.0", "review/text": "Looked all over in art supply and other stores for \"crazy cutting\" scissors for my 4-year old grandson. These are exactly what I was looking for - fun, very well made, metal rather than plastic blades (so they actually do a good job of cutting paper), safe (\"blunt\") ends, etc. (These really are for age 4 and up, not younger.) Very high quality. Very pleased with the product."}{"review/profileName": "L. Heminway", "product/price": "unknown", "review/time": "1156636800", "product/productId": "B000140KIW", "review/helpfulness": "1/1", "review/summary": "Fiskars Softouch Multi-Purpose Scissors, 10\"", "review/userId": "A2M2M4R1KG5WOL", "product/title": "Fiskars Softouch Multi-Purpose Scissors 10\"", "review/score": "5.0", "review/text": "These are the BEST scissors I have ever owned. I am left-handed and take note that either a left or right-handed person can use these equally well.If you have arthritis, as I do, these scissors are amazing as well. Well worth the price. I now own three pairs of these and have convinced many other people in my quilting group that they NEED a pair as well!They cut through muli layers and difficult to cut items really well.Do buy them, you won't regret it!"}{"review/profileName": "R. GARCIA", "product/price": "unknown", "review/time": "1214784000", "product/productId": "B000140KIW", "review/helpfulness": "0/0", "review/summary": "Best scissors ever", "review/userId": "ARQAQ6ZYMFPCA", "product/title": "Fiskars Softouch Multi-Purpose Scissors 10\"", "review/score": "5.0", "review/text": "This Fiskars Scissors are the best i've bougth in time. It are excellent for delicated fabrics and silck ribbon embroidery, also work perfectly with paper."}{"review/profileName": "Dea Carey \"deacarey\"", "product/price": "unknown", "review/time": "1173484800", "product/productId": "B000140KIW", "review/helpfulness": "0/0", "review/summary": "A great tool to make your work easier", "review/userId": "A3FPG4LAJ1HOHZ", "product/title": "Fiskars Softouch Multi-Purpose Scissors 10\"", "review/score": "5.0", "review/text": "I finally gave in and bought these after years of wanting them. I'm so glad I did!I'm a book artist and crafter, and I usually do my creative work in marathon stretches. (You know, nothing for three weeks and then an all-day session...) These scissors make it so much easier. They cut easily and cleanly, with no hand fatigue. They also saved me when I did all my Christmas gift wrapping.I should have just bought these in the first place!"} 

我想進入數據幀具有摘要名稱,價格,ReviewText等列名這。在R中有這麼簡單的方法嗎?

+0

這些軟件包像'rjson'或'jsonlite'這樣的軟件可以幫你做很好的工作。 – drmariod

+0

我一直在嘗試與rjson一段時間。據我所知,沒有做這項工作。 – user3766332

+0

這裏是一個不同的線程和不同的包,這可能有助於[鏈接](http://stackoverflow.com/questions/16947643/getting-imported-json-data-into-a-data-frame-in-r) – drmariod

回答

0

我對你的數據起了一點作用,看起來最後3個數據集有點混亂,我找不到原因。

但前兩個,在將,[]添加到字符串並刪除單引號後,它在我建議的鏈接中工作。

t <- '[{"review/profileName": "Carleen M. Amadio \"Lady Dragonfly\"", "product/price": "unknown", "review/time": "1314057600", "product/productId": "B000GKXY4S", "review/helpfulness": "2/2", "review/summary": "Fun for adults too!", "review/userId": "A1QA985ULVCQOB", "product/title": "Crazy Shape Scissor Set", "review/score": "5.0", "review/text": "I really enjoy these scissors for my inspiration books that I am making (like collage, but in books) and using these different textures these give is just wonderful, makes a great statement with the pictures and sayings. Want more, perfect for any need you have even for gifts as well. Pretty cool!"}, 
{"review/profileName": "Barbara", "product/price": "unknown", "review/time": "1328659200", "product/productId": "B000GKXY4S", "review/helpfulness": "0/0", "review/summary": "Making the cut!", "review/userId": "ALCX2ELNHLQA7", "product/title": "Crazy Shape Scissor Set", "review/score": "5.0", "review/text": "Looked all over in art supply and other stores for \"crazy cutting\" scissors for my 4-year old grandson. These are exactly what I was looking for - fun, very well made, metal rather than plastic blades (so they actually do a good job of cutting paper), safe (\"blunt\") ends, etc. (These really are for age 4 and up, not younger.) Very high quality. Very pleased with the product."}]' 
# {"review/profileName": "L. Heminway", "product/price": "unknown", "review/time": "1156636800", "product/productId": "B000140KIW", "review/helpfulness": "1/1", "review/summary": "Fiskars Softouch Multi-Purpose Scissors, 10\"", "review/userId": "A2M2M4R1KG5WOL", "product/title": "Fiskars Softouch Multi-Purpose Scissors 10\"", "review/score": "5.0", "review/text": "These are the BEST scissors I have ever owned. I am left-handed and take note that either a left or right-handed person can use these equally well.If you have arthritis, as I do, these scissors are amazing as well. Well worth the price. I now own three pairs of these and have convinced many other people in my quilting group that they NEED a pair as well!They cut through muli layers and difficult to cut items really well.Do buy them, you wont regret it!"}, 
# {"review/profileName": "R. GARCIA", "product/price": "unknown", "review/time": "1214784000", "product/productId": "B000140KIW", "review/helpfulness": "0/0", "review/summary": "Best scissors ever", "review/userId": "ARQAQ6ZYMFPCA", "product/title": "Fiskars Softouch Multi-Purpose Scissors 10\"", "review/score": "5.0", "review/text": "This Fiskars Scissors are the best ive bougth in time. It are excellent for delicated fabrics and silck ribbon embroidery, also work perfectly with paper."}, 
# {"review/profileName": "Dea Carey \"deacarey\"", "product/price": "unknown", "review/time": "1173484800", "product/productId": "B000140KIW", "review/helpfulness": "0/0", "review/summary": "A great tool to make your work easier", "review/userId": "A3FPG4LAJ1HOHZ", "product/title": "Fiskars Softouch Multi-Purpose Scissors 10\"", "review/score": "5.0", "review/text": "I finally gave in and bought these after years of wanting them. Im so glad I did!Im a book artist and crafter, and I usually do my creative work in marathon stretches. (You know, nothing for three weeks and then an all-day session...) These scissors make it so much easier. They cut easily and cleanly, with no hand fatigue. They also saved me when I did all my Christmas gift wrapping.I should have just bought these in the first place!"}]' 
require(RJSONIO) 
json_file <- fromJSON(t) 
json_file <- lapply(json_file, function(x) { 
    x[sapply(x, is.null)] <- NA 
    unlist(x) 
}) 
do.call("rbind", json_file) 
1

在你的字符串中有多個JSON對象。我想你應該先拆分它們然後解析它們。對於您的示例字符串(稱之爲x),你可以試試這個:

require(RJSONIO) 
do.call(rbind,lapply(strsplit(x,"(?<=\\})",perl=TRUE)[[1]],fromJSON)) 

您獲得character矩陣可如果你需要它很容易地轉換爲data.frame