我想要什么:我想解析表单的文本文件
{"business_id": "rncjoVoEFUJGCUoC1JgnUA", "full_address": "8466 W Peoria Ave\nSte 6\nPeoria, AZ 85345", "open": true, "categories": ["Accountants", "Professional Services", "Tax Services", "Financial Services"], "city": "Peoria", "review_count": 3, "name": "Peoria Income Tax Service", "neighborhoods": [], "longitude": -112.241596, "state": "AZ", "stars": 5.0, "latitude": 33.581867000000003, "type": "business"}
{"business_id": "0FNFSzCFP_rGUoJx8W7tJg", "full_address": "2149 W Wood Dr\nPhoenix, AZ 85029", "open": true, "categories": ["Sporting Goods", "Bikes", "Shopping"], "city": "Phoenix", "review_count": 5, "name": "Bike Doctor", "neighborhoods": [], "longitude": -112.10593299999999, "state": "AZ", "stars": 5.0, "latitude": 33.604053999999998, "type": "business"}
其中每一行都是一个单独的 json 对象。我希望解析后的表单是 RPart 可以作为参数的类型。
如果我循环遍历每一行,我可以让它工作,但根据这个 SO answer,R 更喜欢使用 apply 函数,而不是单独循环遍历每一行。
问题:当我运行我的代码时,我收到了这个错误
Error in apply(yelp_df, 1, fromJSON) : dim(X) must have a positive length
我的代码
#!/usr/bin/Rscript
require(graphics)
require(RJSONIO)
con <- file("yelp_phoenix_academic_dataset/yelp_academic_dataset_business.json", "r")
yelp_df <- readLines(con) #rather then guessing what the optimal buffer size of the system is I'll just put everything into memeory
apply(yelp_df, 1, fromJSON)