首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用RJSONIO解析一行JSON对象的文本文件

使用RJSONIO解析一行JSON对象的文本文件
EN

Stack Overflow用户
提问于 2013-05-23 03:38:08
回答 1查看 3.7K关注 0票数 2

我想要的:,我想解析表单的文本文件

代码语言:javascript
复制
{"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可以作为参数的类型。

如果循环遍历每一行,我可以让它工作,但是根据这一点,答案是R,更喜欢使用apply函数,而不是单独遍历每一行。

For each row in an R dataframe

问题:当我运行我的代码时,我得到了这个错误

代码语言:javascript
复制
Error in apply(yelp_df, 1, fromJSON) : dim(X) must have a positive length

我的代码

代码语言:javascript
复制
#!/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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-23 07:03:01

readLines正在返回一个字符向量。apply需要一个数组。使用lapply或类似的东西。

代码语言:javascript
复制
out <- lapply(readLines("test.txt"), fromJSON)

> head(out[[1]])
$business_id
[1] "rncjoVoEFUJGCUoC1JgnUA"

$full_address
[1] "8466 W Peoria Ave\nSte 6\nPeoria, AZ 85345"

$open
[1] TRUE

$categories
[1] "Accountants"           "Professional Services" "Tax Services"         
[4] "Financial Services"   

$city
[1] "Peoria"

$review_count
[1] 3
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16705259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档