r-使用jsonlite或Regex访问json值
我试图使用jsonlite包从json访问一个值,但我试图访问的值(期间为0m的键strongbuy的值)具有相同的键名(期间-1m具有相同的键名),因此它返回NA值。有没有办法用jsonlite或regex来实现这一点r-使用jsonlite或Regex访问json值,r,json,regex,jsonlite,R,Json,Regex,Jsonlite,我试图使用jsonlite包从json访问一个值,但我试图访问的值(期间为0m的键strongbuy的值)具有相同的键名(期间-1m具有相同的键名),因此它返回NA值。有没有办法用jsonlite或regex来实现这一点 i<- "MSFT" json_object <- getURL(json_url) quote_summary <- fromJSON(json_object) %>% unlist() quote_summary["quoteSum
i<- "MSFT"
json_object <- getURL(json_url)
quote_summary <- fromJSON(json_object) %>% unlist()
quote_summary["quoteSummary.result.recommendationTrend.trend.strongBuy"] %>% unname() %>% as.numeric()
以下是“报价汇总”的值:
"{\"quoteSummary\":{\"result\":[{\"recommendationTrend\":{\"trend\":[{\"period\":\"0m\",\"strongBuy\":14,\"buy\":13,\"hold\":6,\"sell\":0,\"strongSell\":1},{\"period\":\"-1m\",\"strongBuy\":12,\"buy\":13,\"hold\":7,\"sell\":0,\"strongSell\":1},{\"period\":\"-2m\",\"strongBuy\":12,\"buy\":13,\"hold\":8,\"sell\":0,\"strongSell\":1},{\"period\":\"-3m\",\"strongBuy\":12,\"buy\":13,\"hold\":9,\"sell\":0,\"strongSell\":1}],\"maxAge\":86400}}],\"error\":null}}"
quoteSummary.result.recommendationTrend.trend.period1
"0m"
quoteSummary.result.recommendationTrend.trend.period2
"-1m"
quoteSummary.result.recommendationTrend.trend.period3
"-2m"
quoteSummary.result.recommendationTrend.trend.period4
"-3m"
quoteSummary.result.recommendationTrend.trend.strongBuy1
"14"
quoteSummary.result.recommendationTrend.trend.strongBuy2
"12"
quoteSummary.result.recommendationTrend.trend.strongBuy3
"12"
quoteSummary.result.recommendationTrend.trend.strongBuy4
"12"
quoteSummary.result.recommendationTrend.trend.buy1
"13"
quoteSummary.result.recommendationTrend.trend.buy2
"13"
quoteSummary.result.recommendationTrend.trend.buy3
"13"
quoteSummary.result.recommendationTrend.trend.buy4
"13"
quoteSummary.result.recommendationTrend.trend.hold1
"6"
quoteSummary.result.recommendationTrend.trend.hold2
"7"
quoteSummary.result.recommendationTrend.trend.hold3
"8"
quoteSummary.result.recommendationTrend.trend.hold4
"9"
quoteSummary.result.recommendationTrend.trend.sell1
"0"
quoteSummary.result.recommendationTrend.trend.sell2
"0"
quoteSummary.result.recommendationTrend.trend.sell3
"0"
quoteSummary.result.recommendationTrend.trend.sell4
"0"
quoteSummary.result.recommendationTrend.trend.strongSell1
"1"
quoteSummary.result.recommendationTrend.trend.strongSell2
"1"
quoteSummary.result.recommendationTrend.trend.strongSell3
"1"
quoteSummary.result.recommendationTrend.trend.strongSell4
"1"
quoteSummary.result.recommendationTrend.maxAge
"86400"
不确定为什么要使用
取消列表
。如果没有它,您可以使用
quote_summary$quoteSummary$result$recommendationTrend$trend[[1]]
看起来像
period strongBuy buy hold sell strongSell
1 0m 14 13 6 0 1
2 -1m 12 13 7 0 1
3 -2m 12 13 8 0 1
4 -3m 12 13 9 0 1
并且属于类data.frame
。从这里您可以使用tidyverse
library(tidyverse)
df %>%
filter(period == "-1m") %>%
select(strongBuy)
strongBuy
1 12
如果要取消列出
结果,请使用recursive=F
,这样就不会出现长度为700+的混乱列表:
unlist(quote_summary$quoteSummary$result, recursive = F)
我想我可以这么做
strongbuy <- quote_summary["quoteSummary.result.recommendationTrend.trend.strongBuy1"]
%>% unname()
%>% as.numeric()
strongbuy%unname()
%>%as.numeric()