如何解析R中对应ID的Json

时间:2018-05-08 11:20:55

标签: r json dataframe jsonlite

我有下面提到的数据框:

ID         Json_Data
PA-124     Json String
PA-145     Json String
PA-185     Json String
PA-149     Json String

我想提取与每个ID相对应的所有json字段,如下所述。

我想知道如何针对每个特定ID(即国家/地区)提取一些特定字段。

ID        Name       Country      Code
PA-124    Ajre       Dubai        154
PA-145    Aret       USA          485
PA-185    Cret       Russia       445
PA-149    Oerp       Canada       789

为此,我使用了下面提到的代码,但它只返回了大约1500个ID中的一个ID的值。

我的代码:

    library(RJSONIO)
    library(rjson)
    library(jsonlite)
    library(data.table)


    f <- function(json, id){
      tmp    <- jsonlite::fromJSON(json)
      tmp    <- as.data.frame(tmp)
      tmp$id <- id
      return(tmp)
    }

json_dfs <- 
  mapply(f, Df$Json_Data, Df$ID, SIMPLIFY = FALSE)

Dput:

    structure(list(ID = c("PA-124", "PA-124", "PA-124"), Json_Data = c("{\n  \"hed\": {\n    \"srti\": \"RT1455\"\n  },\n  \"nnaesr\": {\n    \"name\": \"\",\n    \"firt\": \"alex\",\n    \"last\": \"john\",\n    \"birth\": \"26061981\",\n    \"gender\": \"Male\"\n  },\n  \"iteret\": {\n    \"adre\": \"asded14eer\",\n    \"adfe\": \"\",\n    \"sfdet\": \"\",\n    \"sfeet\": \"\",\n    \"serted\": \"\",\n    \"sertgf\": \"\"\n  },\n  \"sefetgd\": {\n    \"mobe\": \"2111111\",\n    \"dfete\": \"\",\n    \"tfet\": \"sdfet\"\n  },\n  \"aarfgeette\": {\n    \"aarfgertte\": \"H NO. , dffr rtrg\",\n    \"aarfgot2\": \"ids School, feeeete,\",\n    \"aarfgot3\": \"\",\n    \"aarfgot4\": \"\",\n    \"aarfgot5\": \"\",\n    \"sdfffy\": \"errtyy\",\n    \"errrte\": \"efeteet\",\n    \"sdetVb\": \"\",\n    \"sdfes\": \"5500\",\n    \"aarfgppr\": \"eretft er\",\n    \"seferf\": \"ered\"\n  }\n}", 
"{\n  \"hed\": {\n    \"srti\": \"RT1455\"\n  },\n  \"nnaesr\": {\n    \"name\": \"\",\n    \"firt\": \"alex\",\n    \"last\": \"john\",\n    \"birth\": \"26061981\",\n    \"gender\": \"Male\"\n  },\n  \"iteret\": {\n    \"adre\": \"asded14eer\",\n    \"adfe\": \"\",\n    \"sfdet\": \"\",\n    \"sfeet\": \"\",\n    \"serted\": \"\",\n    \"sertgf\": \"\"\n  },\n  \"sefetgd\": {\n    \"mobe\": \"2111111\",\n    \"dfete\": \"\",\n    \"tfet\": \"sdfet\"\n  },\n  \"aarfgeette\": {\n    \"aarfgertte\": \"efeet efet\",\n    \"aarfgot2\": \"affe fe,\",\n    \"aarfgot3\": \"\",\n    \"aarfgot4\": \"\",\n    \"aarfgot5\": \"\",\n    \"sdfffy\": \"Telangana\",\n    \"errrte\": \"Hyderabad\",\n    \"sdetVb\": \"\",\n    \"sdfes\": \"5500\",\n    \"aarfgppr\": \"acefec ee\",\n    \"seferf\": \"dfefe\"\n  }\n}", 
"{\n  \"hed\": {\n    \"srti\": \"RT1455\"\n  },\n  \"nnaesr\": {\n    \"name\": \"\",\n    \"firt\": \"alex\",\n    \"last\": \"john\",\n    \"birth\": \"26061981\",\n    \"gender\": \"Male\"\n  },\n  \"iteret\": {\n    \"adre\": \"asded14eer\",\n    \"adfe\": \"\",\n    \"sfdet\": \"\",\n    \"sfeet\": \"\",\n    \"serted\": \"\",\n    \"sertgf\": \"\"\n  },\n  \"sefetgd\": {\n    \"mobe\": \"2111111\",\n    \"dfete\": \"\",\n    \"tfet\": \"sdfet\"\n  },\n  \"aarfgeette\": {\n    \"aarfgertte\": \"ad.de 25dfege\",\n    \"aarfgot2\": \"adfed ,\",\n    \"aarfgot3\": \"\",\n    \"aarfgot4\": \"\",\n    \"aarfgot5\": \"\",\n    \"sdfffy\": \"adffe\",\n    \"errrte\": \"Hyderabad\",\n    \"sdetVb\": \"\",\n    \"sdfes\": \"5500\",\n    \"aarfgppr\": \"sadfe ad\",\n    \"seferf\": \"dsfde\"\n  }\n}"
)), row.names = c(NA, 3L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

您可以使用unlist将其转换为字符向量,然后将字符向量列表行绑定到data.frame中。然后列将结果与您的ID绑定

cbind(temp$ID,
    do.call(rbind, lapply(temp$Json_Data, function(x) unlist(jsonlite::fromJSON(x))))
)

结果:

              hed.srti nnaesr.name nnaesr.firt nnaesr.last nnaesr.birth nnaesr.gender iteret.adre  iteret.adfe iteret.sfdet iteret.sfeet iteret.serted iteret.sertgf
[1,] "PA-124" "RT1455" ""          "alex"      "john"      "26061981"   "Male"        "asded14eer" ""          ""           ""           ""            ""           
[2,] "PA-124" "RT1455" ""          "alex"      "john"      "26061981"   "Male"        "asded14eer" ""          ""           ""           ""            ""           
[3,] "PA-124" "RT1455" ""          "alex"      "john"      "26061981"   "Male"        "asded14eer" ""          ""           ""           ""            ""           
     sefetgd.mobe sefetgd.dfete sefetgd.tfet aarfgeette.aarfgertte aarfgeette.aarfgot2    aarfgeette.aarfgot3 aarfgeette.aarfgot4 aarfgeette.aarfgot5 aarfgeette.sdfffy
[1,] "2111111"    ""            "sdfet"      "H NO. , dffr rtrg"   "ids School, feeeete," ""                  ""                  ""                  "errtyy"         
[2,] "2111111"    ""            "sdfet"      "efeet efet"          "affe fe,"             ""                  ""                  ""                  "Telangana"      
[3,] "2111111"    ""            "sdfet"      "ad.de 25dfege"       "adfed ,"              ""                  ""                  ""                  "adffe"          
     aarfgeette.errrte aarfgeette.sdetVb aarfgeette.sdfes aarfgeette.aarfgppr aarfgeette.seferf
[1,] "efeteet"         ""                "5500"           "eretft er"         "ered"           
[2,] "Hyderabad"       ""                "5500"           "acefec ee"         "dfefe"          
[3,] "Hyderabad"       ""                "5500"           "sadfe ad"          "dsfde"