如何将此API调用解析为(在R中)为.txt表格式?(与以色列的"开放政府"有关:))

Tal*_*ili 5 parsing r

以色列已经发布了所有预算的预算,并且有一个提取数据的API.但是,我不知道如何将其解析为txt/csv格式.

以下是调用数据示例链接.

这是输出:

[
    {
        "parent": [
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 6075053, 
        "year": 2003, 
        "title": "????? ?????", 
        "gross_amount_used": 5942975, 
        "gross_amount_revised": 5942975, 
        "budget_id": "0021", 
        "net_amount_used": 5936491, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 5861591, 
        "gross_amount_allocated": 5861591
    }, 
    {
        "parent": [
            {
                "budget_id": "0021", 
                "title": "????? ?????"
            }, 
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 5364976, 
        "year": 2003, 
        "title": "??????? ?????? ??????? ?????? ?????", 
        "gross_amount_used": 5337585, 
        "gross_amount_revised": 5337584, 
        "budget_id": "002102", 
        "net_amount_used": 5331101, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 4985915, 
        "gross_amount_allocated": 4985915
    }, 
    {
        "parent": [
            {
                "budget_id": "0021", 
                "title": "????? ?????"
            }, 
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 565495, 
        "year": 2003, 
        "title": "??????? ???????", 
        "gross_amount_used": 462490, 
        "gross_amount_revised": 462490, 
        "budget_id": "002103", 
        "net_amount_used": 462490, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 559293, 
        "gross_amount_allocated": 559293
    }, 
    {
        "parent": [
            {
                "budget_id": "0021", 
                "title": "????? ?????"
            }, 
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 0, 
        "year": 2003, 
        "title": "????? ???????????", 
        "gross_amount_used": 0, 
        "gross_amount_revised": null, 
        "budget_id": "002105", 
        "net_amount_used": null, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 171801, 
        "gross_amount_allocated": 171801
    }, 
    {
        "parent": [
            {
                "budget_id": "0021", 
                "title": "????? ?????"
            }, 
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 108000, 
        "year": 2003, 
        "title": "????? ?????? ??????    ?????", 
        "gross_amount_used": 108000, 
        "gross_amount_revised": 108000, 
        "budget_id": "002106", 
        "net_amount_used": 108000, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 108000, 
        "gross_amount_allocated": 108000
    }, 
    {
        "parent": [
            {
                "budget_id": "0021", 
                "title": "????? ?????"
            }, 
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 23634, 
        "year": 2003, 
        "title": "???? ????? ????", 
        "gross_amount_used": 23634, 
        "gross_amount_revised": 23634, 
        "budget_id": "002101", 
        "net_amount_used": 23634, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 23634, 
        "gross_amount_allocated": 23634
    }, 
    {
        "parent": [
            {
                "budget_id": "0021", 
                "title": "????? ?????"
            }, 
            {
                "budget_id": "00", 
                "title": "??????"
            }
        ], 
        "net_amount_revised": 12948, 
        "year": 2003, 
        "title": "?????? ?? ??????       ??????? ?????", 
        "gross_amount_used": 11266, 
        "gross_amount_revised": 11266, 
        "budget_id": "002104", 
        "net_amount_used": 11266, 
        "inflation_factor": 1.15866084989269, 
        "net_amount_allocated": 12948, 
        "gross_amount_allocated": 12948
    }
]
Run Code Online (Sandbox Code Playgroud)

将此解析为表格格式的方法是什么?

谢谢!

塔尔

Noa*_*oah 2

是的,它是 JSON。fromJSON会把它变成一个清单给你

resp <- getURL("http://budget.yeda.us/0021?year=2003&depth=1")
library(rjson)
resp <- fromJSON(resp)
Run Code Online (Sandbox Code Playgroud)

这会让你列出表格。对于数据框,请尝试:

library(plyr)
resp <- llply(resp, function(x) llply(x, function(y) ifelse(is.null(y), "NULL", y)))
budget <- data.frame()
for(i in 1:length(resp)) {
  budget <- rbind.fill(budget, data.frame(resp[[i]]))
}
Run Code Online (Sandbox Code Playgroud)

在创建包含空值的数据框时,嵌套llply可以解决一些令人不快的问题。