我正在尝试将JSON文件读入R但我收到此错误:
Error in parseJSON(txt) : parse error: trailing garbage
[ 33.816101, -117.979401 ] } { "a": "Mozilla\/4.0 (compatibl
(right here) ------^
Run Code Online (Sandbox Code Playgroud)
我从http://1usagov.measuredvoice.com/下载了该文件并使用7zip解压缩,然后我在R中使用了以下代码:
library(jsonlite)
jsonData <- fromJSON("usagov_bitly_data2013-05-17-1368832207")
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会出现这种错误,我在Google上查了一下,但没有任何信息,有人可以帮我吗?这是文件问题还是我的代码?
我正在尝试将json文件加载到r中的data.frame中.我对jsonlite包中的fromJSON函数运气不错 - 但我得到了嵌套列表,并且不确定如何将输入展平为二维data.frame.Jsonlite以data.frame的形式读取文件,但在一些变量中留下嵌套列表.
有人在使用嵌套列表读入时将任何JSON文件加载到data.frame有任何提示.
#*#*#*#*#*#*#*#*#*##*#*#*#*#*#*#*#*#*# HERE IS MY EXAMPLE #*#*#*#*#*#*#*#*#*##*#*#*#*#*#*#*#*#*#
# loads the packages
library("httr")
library( "jsonlite")
# downloads an example file
providers <- fromJSON( "http://fm.formularynavigator.com/jsonFiles/publish/11/47/providers.json" , simplifyDataFrame=TRUE )
# the flatten function breaks the name variable into three vars ( first name, middle name, last name)
providers <- flatten( providers )
# but many of the columns are still lists:
sapply( providers , class)
# Some of these lists have a single level
head( providers$facility_type )
# Some have …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码示例:
library(jsonlite)
library(curl)
#url
url = "http://www.zillow.com/search/GetResults.htm?spt=homes&status=001000<=000000&ht=010000&pr=999999,10000001&mp=3779,37788&bd=0%2C&ba=0%2C&sf=,&lot=0%2C&yr=,1800&singlestory=0&hoa=0%2C&pho=0&pets=0&parking=0&laundry=0&income-restricted=0&pnd=0&red=0&zso=0&days=36m&ds=all&pmf=0&pf=0&sch=100111&zoom=6&rect=-91307373,29367814,-84759521,35554574&p=1&sort=globalrelevanceex&search=maplist&rid=4&rt=2&listright=true&isMapSearch=true&zoom=6"
#json
results_data_json = fromJSON(txt = url)
Run Code Online (Sandbox Code Playgroud)
我曾经能够运行类似的代码,没有任何问题.现在我收到以下错误:
Error in feed_push_parser(buf) :
lexical error: invalid char in json text.
<html><head><title>Zillow: Real
(right here) ------^
Run Code Online (Sandbox Code Playgroud)
围绕这个想法吗?
我有一个看起来像这样的日志文件.
我试图通过以下方式解析Message列中的JSON :
library(readr)
library(jsonlite)
df <- read_csv("log_file_from_above.csv")
fromJSON(as.character(df$Message))
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了以下错误:
Error: parse error: trailing garbage
"isEmailConfirmed": false } { "id": -1, "firstName":
(right here) ------^
Run Code Online (Sandbox Code Playgroud)
我怎样才能摆脱"尾随垃圾"?
使用PostgreSQL 9.4,新的数据类型JSONB已经可用.根据在单个节点上提供的性能测试,性能优于mongodb(可以理解的是mongodb的最佳位置).
是否可以在R中读取JSONB,理想情况下使用jsonlite?
怎么样?
我试图将从API中提取的JSON转换为R中的数据帧,以便我可以使用和分析数据.
#Install needed packages
require(RJSONIO)
require(httr)
#request a list of companies currently fundraising using httr
r <- GET("https://api.angel.co/1/startups?filter=raising")
#convert to text object using httr
raise <- content(r, as="text")
#convert to list using RJSONIO
fromJSON(raise) -> new
Run Code Online (Sandbox Code Playgroud)
一旦我得到这个对象,new我就很难将列表解析成数据帧.json有这样的结构:
{
"startups": [
{
"id": 6702,
"name": "AngelList",
"quality": 10,
"...": "...",
"fundraising": {
"round_opened_at": "2013-07-30",
"raising_amount": 1000000,
"pre_money_valuation": 2000000,
"discount": null,
"equity_basis": "equity",
"updated_at": "2013-07-30T08:14:40Z",
"raised_amount": 0.0
}
}
],
"total": 4268 ,
"per_page": 50,
"page": 1,
"last_page": …Run Code Online (Sandbox Code Playgroud) 我有json包含国家数据的文件.其中一个文件包含以下数据:
"[{\"count\":1,\"subject\":{\"name\":\"Namibia\",\"alpha2\":\"NA\"}}]"
Run Code Online (Sandbox Code Playgroud)
我将以下代码转换json为data.frame使用jsonlite包:
df = as.data.frame(fromJSON(jsonfile), flatten=TRUE))
Run Code Online (Sandbox Code Playgroud)
我期待一个data.frame数字和字符串:
count subject.name subject.alpha2
1 Namibia "NA"
Run Code Online (Sandbox Code Playgroud)
相反,NAalpha2代码会自动转换为NA逻辑,这就是我得到的:
str(df)
$ count : int 1
$ subject.name : chr "Namibia"
$ subject.alpha2: logi NA
Run Code Online (Sandbox Code Playgroud)
我希望alpha2是一个字符串,而不是逻辑.我该如何解决?
我正在努力创建嵌套/分层 JSON 文件。实际上,我的文件将在不同级别(从零个子节点到多个子节点)具有不同数量的子节点,并且树中的每个“节点”将具有相同的键:值对:名称、ID、类型。记住这一点,我从 R 到 JSON 的输出应该类似于:
{"name": "I",
"id": "001",
"type": "roman",
"children": [
{"name": "1",
"id": "002",
"type": "arabic",
"children": [
{"name": "A",
"id": "003",
"type": "alpha-U"},
{"name": "B",
"id": "004",
"type": "alpha-U"}
]},
{"name": "2",
"id": "005",
"type": "arabic",
"children": [
{"name": "C",
"id": "005",
"type": "alpha-U"},
{"name": "D",
"id": "006",
"type": "alpha-U"}
]}
]}
Run Code Online (Sandbox Code Playgroud)
我试过从列表中创建 JSON。我知道我在这里的某个地方需要一个数据框,但我不知道如何做到这一点。
这段代码让我接近:
mylist <- list(name="I", id="001", type="roman",
children=list(name="1", id="002", type="arabic",
children=list(name="A", id="003", type="alpha-U")
))
jsonlite::toJSON(mylist, pretty=TRUE, auto_unbox=TRUE)
Run Code Online (Sandbox Code Playgroud)
导致此输出:
{
"name": "I", …Run Code Online (Sandbox Code Playgroud) 考虑这个闪亮的应用程序:
library(shiny)
library(ggplot2)
ui <- fluidPage(
radioButtons("type", "Type of plot", choices = c("density", "boxplot")),
plotOutput("plot")
)
server <- function(input, output){
output[["plot"]] <- renderPlot({
if(input$type == "density"){
ggplot(iris, aes(Sepal.Length)) + geom_density()
}else{
ggplot(iris, aes(x = "", y = Sepal.Length)) + geom_boxplot()
}
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
当我选择单选按钮“boxplot”时,来自jsonlite包的这条消息出现在 R 控制台中:
asJSON(keep_vec_names=TRUE) 的输入是一个命名向量。在未来的 jsonlite 版本中,将不支持此选项,命名向量将被转换为数组而不是对象。如果您想要 JSON 对象输出,请改用命名列表。见?toJSON。
我想了解发生了什么。我应该怎么做才不会收到这条消息?我担心我的应用程序会被未来版本的jsonlite.
我需要将数据帧转换为 JSON。数据框中有几个嵌套的数据框作为变量来转换为 JSON。
但是,当转换为 JSON 时,我需要将下面描述的数据Values1作为对象({}仅包含在 中)而不是数组(包含在 中[])。
下面的代码是一个表示,显示我当前的工作流程和问题。
library(dplyr)
library(tidyr)
library(jsonlite)
df1 <- data.frame(name = c("a", "b", "c"),
v = c(1, 2, 3),
w = c(10, 20, 30)) %>%
group_by(name) %>%
nest_legacy(.key = "Values1")
df2 <- data.frame(name = c("a", "b", "c"),
x = c(5, 10, 15),
y = c(100, 200, 300),
z = c(1000, 2000, 3000)) %>%
group_by(name) %>%
nest_legacy(.key = "Values2")
df3 <- df1 %>%
left_join(df2)
json <- toJSON(df3, dataframe = "rows", …Run Code Online (Sandbox Code Playgroud)