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

这是一个example link to make a call for data

这是输出:

[
    {
        "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
    }
]

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

谢谢!

塔尔

最佳答案

是的,它是JSON。 fromJSON将其转换为您的列表

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

这使您可以列出表单。对于数据帧,请尝试:
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]]))
}

创建包含空值的数据帧时,嵌套的llply会解决一些不愉快的情况。

关于parsing - 如何将(在R中)此API调用解析为.txt表格式? (与以色列的 "open government"有关:) ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5351907/

10-12 14:59