【发布时间】:2021-08-09 20:12:13
【问题描述】:
我有一个包含多个参与者的三个主要任务(输入 ID 信息和两个行为任务)的嵌套 JSON 数据的 txt 文件。我希望能够完全展平这个文件,并在最后创建一个 excel 文件,其中包含第一个字符串 (rt 33588) 和第二个字符串 (rt 33358 + "success....) 和第二个 excel 中的展平信息带有第一个字符串的文件 (rt 3358 + "success...)
我试过了:
require(pacman)
p_load('tidyverse','jsonlite')
read_file('jsondata.txt') %>%
str_split('\n') %>% first() %>%
discard(function(x) x == '') %>%
map_dfr(fromJSON, flatten=T) -> mydata
但是这会返回错误代码:
Error: Can't combine `..1$responses` <character> and `..2$responses` <list>.
这似乎是一个 tidyverse 错误,我不知道如何解决。如果我手动拆分 txt 文件,我可以在三个字符串中的每一个上运行上述代码并创建两个 excel 文件,但是必须有一种方法可以读取单个 txt 文件并将其拆分?
我的 JSON 数据如下:
[{"rt":33588,"responses":"{\"School_ID\":\"12345\",\"Class_ID\":\"12345\"}","trial_type":"survey-text-image","trial_index":0,"time_elapsed":33606,"internal_node_id":"0.0-0.0"},{"rt":4424,"responses":"{\"Participant_ID\":\"1833\"}","trial_type":"survey-text-image","trial_index":1,"time_elapsed":38064,"internal_node_id":"0.0-1.0-0.0-0.0"}]
[{"success":true,"timeout":false,"failed_images":[],"failed_audio":[],"failed_video":[],"trial_type":"preload","trial_index":0,"time_elapsed":152,"internal_node_id":"0.0-0.0"},{"rt":6004,"stimulus":"<div><h1> Task 1 </h1></div></div>","response":0,"trial_type":"html-button-response-modified","trial_index":1,"time_elapsed":6172,"internal_node_id":"0.0-1.0"},{"responses":[{"image_id":"p_ts1","image":"img/target.png","rt":1062,"correct":true,"coord_x":"501","coord_y":"205","target":"true","side":"C","height":"34.5","width":"33.7"},{"image_id":"p_ts2","image":"img/target.png","rt":1444,"correct":true,"coord_x":"501","coord_y":"258","target":"true","side":"C","height":"34.5","width":"33.7"},{"image_id":"p_ts3","image":"img/target.png","rt":2177,"correct":true,"coord_x":"501","coord_y":"507","target":"true","side":"C","height":"34.5","width":"33.7"}],"trial_type":"target-cancellation","trial_index":2,"time_elapsed":10108,"internal_node_id":"0.0-2.0-0.0"},{"rt":1003,"stimulus":"<div><h1> Main Task</h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":3,"time_elapsed":11123,"internal_node_id":"0.0-3.0"},{"responses":[{"image_id":"ts1","image":"img/target.png","rt":1264,"correct":true,"coord_x":"932","coord_y":"39","target":"true","side":"R","height":"34.5","width":"33.7"},{"image_id":"ts2","image":"img/target.png","rt":1484,"correct":true,"coord_x":"880","coord_y":"77","target":"true","side":"R","height":"34.5","width":"33.7"},{"image_id":"ts11","image":"img/target.png","rt":2039,"correct":true,"coord_x":"744","coord_y":"53","target":"true","side":"R","height":"34.5","width":"33.7"}],"trial_type":"target-cancellation","trial_index":4,"time_elapsed":27258,"internal_node_id":"0.0-4.0"},{"rt":1713.0000000000036,"stimulus":"<div><h1> Finish Task</h1><img id='stars' src='img/target.png'></img></div>","response":0,"trial_type":"html-button-response-modified","trial_index":5,"time_elapsed":28984,"internal_node_id":"0.0-5.0"}]
[{"success":true,"timeout":false,"failed_images":[],"failed_audio":[],"failed_video":[],"trial_type":"preload","trial_index":0,"time_elapsed":156,"internal_node_id":"0.0-0.0"},{"rt":1794,"stimulus":"<div><h1> Task 2</h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":1,"time_elapsed":1959,"internal_node_id":"0.0-1.0"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":2,"time_elapsed":5311,"internal_node_id":"0.0-2.0-0.0"},{"rt":1082.999999999999,"stimulus":"img/target.png","response":1,"test_part":"Practice Test","condition":"Medium","type":"Large","target":"two","correct_response":1,"trial_type":"image-button-response","trial_index":3,"time_elapsed":6404,"internal_node_id":"0.0-2.0-1.0","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":4,"time_elapsed":7411,"internal_node_id":"0.0-2.0-2.0"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":5,"time_elapsed":13421,"internal_node_id":"0.0-2.0-0.1"},{"rt":971,"stimulus":"img/SmallSun.png","response":0,"test_part":"Practice Test","condition":"Extra Long","type":"Small","target":"left","correct_response":0,"trial_type":"image-button-response","trial_index":6,"time_elapsed":14421,"internal_node_id":"0.0-2.0-1.1","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":7,"time_elapsed":15429,"internal_node_id":"0.0-2.0-2.1"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":8,"time_elapsed":16577,"internal_node_id":"0.0-2.0-0.2"},{"rt":1000,"stimulus":"img/SmallStar.png","response":1,"test_part":"Practice Test","condition":"Short","type":"Small","target":"right","correct_response":1,"trial_type":"image-button-response","trial_index":9,"time_elapsed":17587,"internal_node_id":"0.0-2.0-1.2","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":10,"time_elapsed":18594,"internal_node_id":"0.0-2.0-2.2"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":11,"time_elapsed":21369,"internal_node_id":"0.0-2.0-0.3"},{"rt":1037,"stimulus":"img/MediumSun.png","response":0,"test_part":"Practise Test","condition":"Medium","type":"Medium","target":"right","correct_response":0,"trial_type":"image-button-response","trial_index":12,"time_elapsed":22415,"internal_node_id":"0.0-2.0-1.3","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":13,"time_elapsed":23428,"internal_node_id":"0.0-2.0-2.3"},{"rt":1621,"stimulus":"<div><h1> Main Task </h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":20,"time_elapsed":33659,"internal_node_id":"0.0-3.0"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"fixation","trial_type":"image-button-response","trial_index":21,"time_elapsed":37990,"internal_node_id":"0.0-4.0-0.0"},{"rt":998,"stimulus":"img/target.png","response":0,"test_part":"test","condition":"Medium","type":"Medium","target":"left","correct_response":0,"trial_type":"image-button-response","trial_index":22,"time_elapsed":39004,"internal_node_id":"0.0-4.0-1.0","correct":true},{"rt":886,"stimulus":"img/target.png","response":0,"test_part":"test","condition":"Long","type":"Medium","target":"left","correct_response":0,"trial_type":"image-button-response","trial_index":80,"time_elapsed":147593,"internal_node_id":"0.0-4.0-1.29","correct":true},{"rt":1369,"stimulus":"<div><h1> End Task Race </h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":99,"time_elapsed":180056,"internal_node_id":"0.0-5.0"}]
非常感谢任何帮助 - 我通常不使用 JSON 数据,因此任何指导都非常有帮助!
【问题讨论】: