如何用R检索json的“值”?

问题描述 投票:0回答:2

所以我尝试了任何东西来获得一个特定的关键:价值。但似乎我无法找到解决方案。有帮助吗?

{  
   "contracts":[  
      {  
         "contractid":"BemDRHtv17",
         "cid":"",
         "category":"WORK",
         "mainCategory":"Grundeinkommen",
         "configured":false,
         "customMainCategory":null,
         "customSubCategory":null,
         "customContractPartner":null,
         "amount":209200,
         "interval":"MONTHLY",
         "runTime":null,
         "periodOfNotice":null,
         "cancelationAlert":null,
         "extensionPeriod":null,
         "contractPartner":{  
            "creditorId":null,
            "name":null,
            "__typename":"ContractPartner"
         },
         "__typename":"Contract"
      },
P.s i'm trying to access the key and value of mainCategory / search for its specific values.
json r key-value
2个回答
1
投票

您可以使用library(jqr)访问原始JSON的特定元素,而不是将整个JSON转换为R对象

library(jqr)

jq(js, ".contracts[].mainCategory")

# "Grundeinkommen"

Data

js <- '{  
   "contracts":[  
{  
"contractid":"BemDRHtv17",
"cid":"",
"category":"WORK",
"mainCategory":"Grundeinkommen",
"configured":false,
"customMainCategory":null,
"customSubCategory":null,
"customContractPartner":null,
"amount":209200,
"interval":"MONTHLY",
"runTime":null,
"periodOfNotice":null,
"cancelationAlert":null,
"extensionPeriod":null,
"contractPartner":{  
"creditorId":null,
"name":null,
"__typename":"ContractPartner"
},
"__typename":"Contract"
}
]}
'

0
投票

您可以使用以下方法访问某些密钥

使用像rjsonjsonlite这样的库

使用fromJSON()函数将json数据调用到R中。让我们说你保存了这样的数据

library(jsonlite)
jsonData = fromJSON("PATH")  #Link to the file or html link

现在,您可以使用此方法解析您的内容,并将结果保存在矩阵(或您要使用的任何数据类)中

variable <- as.matrix(jsonData$contracts$mainCategory)
© www.soinside.com 2019 - 2024. All rights reserved.