将json转换为csv文件时出现错误消息

时间:2016-07-11 18:38:41

标签: json r csv

我在R中看到了几篇关于将json转换为csv的帖子,但我有 这个错误:

  

fromJSON中的错误(file =" C:/users/emily/destop/data.json"):参数" txt"在运行以下代码后缺少,没有默认值。

mydf <- fromJSON(file= "C:/users/emily/destop/data.json")

我下载了jsonlite个包。

该命令似乎没有正确读取json数据或无法将其转换为csv文件。

来自data.json的一些示例数据如下所示:

{"reviewerID": "A3TS466QBAWB9D", "asin": "0014072149", "reviewerName": "Silver Pencil", "helpful": [0, 0], "reviewText": "If you are a serious violin student on a budget, this edition has it all: Piano accompaniment, low price, urtext solo parts, and annotations by Maestro David Oistrakh where Bach's penmanship is hard to decipher.  Additions (in dashes) are easily distinguishable from the original bowings.  This is a delightful concerto that all intermediate level violinists should perform with a violin buddy.  Get your copy, today, along with \"The Green Violin; Theory, Ear Training, and Musicianship for Violinists\" book to prepare for this concerto and for more advanced playing!", "overall": 5.0, "summary": "Perform it with a friend, today!", "unixReviewTime": 1370476800, "reviewTime": "06 6, 2013"}
{"reviewerID": "A3BUDYITWUSIS7", "asin": "0041291905", "reviewerName": "joyce gabriel cornett", "helpful": [0, 0], "reviewText": "This is and excellent edition and perfectly true to the orchestral version!  It makes playing Vivaldi a joy!  I uses this for a wedding and was totally satisfied with the accuracy!", "overall": 5.0, "summary": "Vivalldi's Four Seasons", "unixReviewTime": 1381708800, "reviewTime": "10 14, 2013"}
{"reviewerID": "A2HR0IL3TC4CKL", "asin": "0577088726", "reviewerName": "scarecrow \"scarecrow\"", "helpful": [0, 0], "reviewText": "this was written for Carin Levine in 2008, but not premiered until 2011 at the Musica Viva Fest in Munich. .the work's premise maybe about the arduousness, nefarious of existence, how we all \"Work\" at life, at complexity, at densities of differing lifeworlds. Ferneyhough's music might suggest that these multiple dimensions of an ontology exist in diagonals across differing spectrums of human cognition, how we come to think about an object,aisthetic shaped ones, and expressionistic ones as his music.The work has a nice classical shape,and holds the \"romantic\" at bay; a mere 7 plus minutes for Alto Flute, a neglected wind cadre member.The work has gorgeous arresting moments with a great bounty of extended timbres-pointillistic bursts\" Klangfarben Sehr Kraeftig\" that you do grow weary of; it is almost predictable now hearing these works; you know they will inhabit a dense timbral space of mega-speed lines tossed in all registers;still one listens; that gap Freud speaks about, that we know we need this at some level. . .the music slowed at times for structural rigour.. .  we have a dramatic causality at play in the subject,(the work's title) the arduousness of pushing, aspiring, working toward something; pleasurable illuminating or not, How about emancipation from itself;I guess we need forget the production of surplus value herein,even with the rebellions in current urban areas today; It has no place. . .  these constructions  are leftovers from modernity, the \"gods\" still hover. . .\"gods\" that Ferneyhough has no power to dispel. . . . All are now commonplace for the new music literature of music.This music still sound quite stunning, marvelous and evocative today,it is simple at some level, direct, unencumbered and violent with spit-tongues,gratuitous lines, fluttertongue,percussive slap-keys,tremoli wistful glissandi harmonics, fast filigreed lines, and simply threadbare melos, an austere fragment of what was a melody. . .Claudio Arrau said someplace that the performer the musician must emanate a \"work\" while playing music, a \"struggle\", aesthetic or otherwise, Sviatoslav Richter thought this grotesque, to look at a musician playing the great music. It was ugly for him. . .You can hear Ms.Levine on youtube playing her work, she is quite convincing, you always need to impart an authority,succored in an emotive focus that the music itself has not succumbed to your own possession. You play the music, it doesn't \"play\" you. . . I'd hope though that music with this arduous construction and structural vigour that it would in fact come to possess the performer. . .it is one of the last libidinal pleasures remaining. . .", "overall": 5.0, "summary": "arduous indeed!", "unixReviewTime": 1371168000, "reviewTime": "06 14, 2013"}
{"reviewerID": "A2DHYD72O52WS5", "asin": "0634029231", "reviewerName": "Amazon Customer \"RCC\"", "helpful": [0, 0], "reviewText": "Greg Koch is a knowledgable and charismatic host. He is seriously fun to watch. The main problem with the video is the format. The lack of on-screen tab is a serious flaw. You have to watch very carefully, have a good understanding of the minor pentatonic, and  basic foundation of blues licks to even have a chance at gleening anything from this video.If you're just starting out, pick up the IN THE STYLE OF series. While this series has its limitations (incomplete songs due to copyright, no doubt), it has on screen tab and each lick is played at a reasonably slow speed. In addition, their web site has downloadable tab.However, if you can hold your own in the minor pentatonic, give this a try. It is quite a workout and you'll find yourself a better player having taken on the challenge.", "overall": 3.0, "summary": "GREAT! BUT NOT FOR  BEGINNERS.", "unixReviewTime": 1119571200, "reviewTime": "06 24, 2005"}
{"reviewerID": "A1MUVHT8BONL5K", "asin": "0634029347", "reviewerName": "Amazon Customer \"clapton music fan\"", "helpful": [2, 12], "reviewText": "I bought this DVD and I'm returning it.  The description and editorial review are misleading.  This is NOT a Clapton video.  Certainly some clips from Clapton, but generally this is a \"how to\" video.  Same applies to Clapton  The Early Years!", "overall": 2.0, "summary": "NOT CLAPTION MUSIC VIDEO!  A Learn How To Play Guitar LIKE Clapton", "unixReviewTime": 1129334400, "reviewTime": "10 15, 2005"}

最终我想正确读取数据并将数据转换为csv文件。

1 个答案:

答案 0 :(得分:2)

The following should work

library(RJSONIO)

# test2.json is a sample json file
# I removed the reviewText field to keep it short
# Also tested this out with 1 row of data
D2 <- RJSONIO::fromJSON("./test2.json")

# convert the numeric vector helpful to one string
D2$helpful <- paste(D2$helpful, collapse = " ")

D2
     reviewerID       asin         reviewerName    helpful
[1,] "A3TS466QBAWB9D" "0014072149" "Silver Pencil" "0 0"  

D3 <- do.call(cbind, D2)

write.csv(D3, "D3.csv")