【问题标题】:r- Accessing json value using jsonlite or Regexr- 使用 jsonlite 或 Regex 访问 json 值
【发布时间】:2018-04-24 10:57:34
【问题描述】:

我正在尝试使用 jsonlite 包从 json 访问一个值,但是我尝试访问的值(键 strongbuy 的值,其中句点为 0m)具有相同的键名(句点 -1m 具有相同的键name),所以它返回 NA 值。有没有办法用 jsonlite 或正则表达式来做到这一点?

i<- "MSFT"
    json_object <- getURL(json_url)
    quote_summary <- fromJSON(json_object) %>% unlist()

quote_summary["quoteSummary.result.recommendationTrend.trend.strongBuy"] %>% unname() %>% as.numeric()

下面是json_object的值:

"{\"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}}"

quote_summary的值如下:

    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" 

【问题讨论】:

    标签: r json regex jsonlite


    【解决方案1】:

    不确定您为什么使用unlist。没有它,您可以使用

    访问相应的表
    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
    

    如果你想unlist 结果,那么使用recursive = F 这样你就不会得到一个长度超过 700+ 的混乱列表:

    unlist(quote_summary$quoteSummary$result, recursive = F)
    

    【讨论】:

      【解决方案2】:

      我想我可以做到这一点。

          strongbuy <- quote_summary["quoteSummary.result.recommendationTrend.trend.strongBuy1"] 
                                            %>% unname() 
                                            %>% as.numeric()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-11
        • 2016-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多