我有一个层次非常深的JSON文件(例如https://raw.githubusercontent.com/APSIMInitiative/ApsimX/master/Models/Resources/Wheat.json)
# read data
library(jsonlite)
m <- read_json('https://raw.githubusercontent.com/APSIMInitiative/ApsimX/master/Models/Resources/Wheat.json')
我想找到一个名称==“ PotentialBranchingRate”的元素。
{
"$type": "Models.Functions.PhaseLookup, Models",
"Name": "PotentialBranchingRate",
"Children": [...]
}
预期结果将返回"Name": "PotentialBranchingRate"
的父节点。
List of 2
$ $type: chr "Models.Functions.PhaseLookup, Models"
$ Name : chr "PotentialBranchingRate"
$ Children : list ...
我已经在包rlist和purrr中搜索了函数,但是找不到任何方法。
有解决此问题的建议吗?
答案 0 :(得分:1)
library(data.tree)
x <- as.Node(m)
Traverse(x, filterFun = function(.)
identical(GetAttribute(., "Name"), "PotentialBranchingRate")
)
#> [[1]]
#> levelName
#> 1 2
#> 2 °--Children
#> 3 ¦--1
#> 4 ¦ °--Children
#> 5 ¦ °--1
#> 6 ¦ °--Children
#> 7 ¦ °--1
#> 8 ¦ ¦--X
#> 9 ¦ ¦--Y
#> 10 ¦ °--Children
#> 11 °--2
#> 12 °--Children
#> 13 °--1
#> 14 °--Children
#>
#> [[2]]
#> levelName
#> 1 1
#> 2 °--Children
#> 3 °--1
#> 4 ¦--X
#> 5 ¦--Y
#> 6 °--Children
res <- .Last.value
lapply(res, function(.) jsonlite::prettify(rjson::toJSON(as.list(.))))
#> [[1]]
#> {
#> "$type": "Models.Functions.PhaseLookup, Models",
#> "Enabled": true,
#> "IncludeInDocumentation": true,
#> "Name": "PotentialBranchingRate",
#> "ReadOnly": false,
#> "Children": {
#> "1": {
#> "$type": "Models.Functions.PhaseLookupValue, Models",
#> ....
#>
#> ....... (truncated output for clarity)
#>
#> [[2]]
#> {
#> "$type": "Models.Functions.LinearInterpolationFunction, Models",
#> "Enabled": true,
#> "IncludeInDocumentation": true,
#> "Name": "PotentialBranchingRate",
#> "ReadOnly": false,
#> "XProperty": "[Structure].LeafTipsAppeared",
#> "Children": {
#> "1": {
#> "$type": "Models.Functions.XYPairs, Models",
#> "Enabled": true,
#> "IncludeInDocumentation": true,
#> "Name": "XYPairs",
#> "ReadOnly": false,
#> "X": {
#> "1": 1,
#> "2": 2,
#> "3": 3,
#> "4": 4,
#> "5": 5,
#> "6": 6,
#> "7": 7,
#> "8": 8
#> },
#> "Y": {
#> "1": 0,
#> "2": 0,
#> "3": 1,
#> "4": 2,
#> "5": 4,
#> "6": 7,
#> "7": 12,
#> "8": 20
#> },
#> "Children": [
#>
#> ]
#> }
#> }
#> }
#>
由reprex package(v0.2.1)于2019-02-21创建
答案 1 :(得分:1)
library(curl)
library(jqr)
url <- paste0(
'https://raw.githubusercontent.com/',
'APSIMInitiative/ApsimX/master/Models/Resources/Wheat.json'
)
curl(url) %>%
jq('.. | select(.Name? == "PotentialBranchingRate")')
#> [
#> {
#> "$type": "Models.Functions.PhaseLookup, Models",
#> "Name": "PotentialBranchingRate",
#> "Children": [
#> {
#> "$type": "Models.Functions.PhaseLookupValue, Models",
#> "Start": "Emergence",
#> "End": "TerminalSpikelet",
#> "Name": "Vegetative",
#> "Children": [
#> {
#> "$type": "Models.Functions.LinearInterpolationFunction, Models",
#> "XProperty": "[Structure].LeafTipsAppeared",
#> "Name": "PotentialBranchingRate",
#> "Children": [
#> {
#> "$type": "Models.Functions.XYPairs, Models",
#> "X": [
#> 1,
#> 2,
#> 3,
#> 4,
#> 5,
#> 6,
#> 7,
#> 8
#> ],
#> "Y": [
#> 0,
#> 0,
#> 1,
#> 2,
#> 4,
#> 7,
#> 12,
#> 20
#> ],
#> "Name": "XYPairs",
#> "Children": [
#>
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> }
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> }
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> },
#> {
#> "$type": "Models.Functions.PhaseLookupValue, Models",
#> "Start": "TerminalSpikelet",
#> "End": "HarvestRipe",
#> "Name": "Reproductive",
#> "Children": [
#> {
#> "$type": "Models.Functions.Constant, Models",
#> "FixedValue": 0,
#> "Units": null,
#> "Name": "Zero",
#> "Children": [
#>
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> }
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> }
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> },
#> {
#> "$type": "Models.Functions.LinearInterpolationFunction, Models",
#> "XProperty": "[Structure].LeafTipsAppeared",
#> "Name": "PotentialBranchingRate",
#> "Children": [
#> {
#> "$type": "Models.Functions.XYPairs, Models",
#> "X": [
#> 1,
#> 2,
#> 3,
#> 4,
#> 5,
#> 6,
#> 7,
#> 8
#> ],
#> "Y": [
#> 0,
#> 0,
#> 1,
#> 2,
#> 4,
#> 7,
#> 12,
#> 20
#> ],
#> "Name": "XYPairs",
#> "Children": [
#>
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> }
#> ],
#> "IncludeInDocumentation": true,
#> "Enabled": true,
#> "ReadOnly": false
#> }
#> ]
由reprex package(v0.2.1)于2019-02-21创建