阅读来自 JSON 的问题
Read issue from JSON
我在这里存储了 xml 个文件。它包含 json。我无法读取并将其转换为数据帧
清扫码
htext <- html_nodes(content, xpath=".//script[contains(., 'home_js_model')]") %>% html_text()
htext <- gsub("<script type=\"text/javascript\">", "", htext, fixed=TRUE)
htext <- gsub("var home_js_model = {", "", htext, fixed=TRUE)
htext <- gsub("</script>", "", htext, fixed=TRUE)
htext <- gsub("stock\":", "", htext, fixed=TRUE)
阅读自JSON
json <- jsonlite::fromJSON(htext)
我也试过了,但没有成功。
jsonlite::stream_in(textConnection(gsub("\n", "", htext)))
你快到了。您需要从开头 trim 关闭 var home_js_model =
并从结尾关闭分号以解析 json。然而,结果是一个非常长、非常复杂的嵌套列表,所以您的解析问题可能才刚刚开始...
jsonlite::fromJSON(substr(htext, 21, 5615711))
#> $stock
#> $stock$period_title
#> [1] "1T2018"
#>
#> $stock$total
#> [1] 4162848
#>
#> $stock$total_sale
#> [1] 3426559
#>
#> $stock$total_rental
#> [1] 736289
#>
#> $stock$total_es
#> [1] 2196851
#>
#> ... (very very long list)
我在这里存储了 xml 个文件。它包含 json。我无法读取并将其转换为数据帧
清扫码
htext <- html_nodes(content, xpath=".//script[contains(., 'home_js_model')]") %>% html_text()
htext <- gsub("<script type=\"text/javascript\">", "", htext, fixed=TRUE)
htext <- gsub("var home_js_model = {", "", htext, fixed=TRUE)
htext <- gsub("</script>", "", htext, fixed=TRUE)
htext <- gsub("stock\":", "", htext, fixed=TRUE)
阅读自JSON
json <- jsonlite::fromJSON(htext)
我也试过了,但没有成功。
jsonlite::stream_in(textConnection(gsub("\n", "", htext)))
你快到了。您需要从开头 trim 关闭 var home_js_model =
并从结尾关闭分号以解析 json。然而,结果是一个非常长、非常复杂的嵌套列表,所以您的解析问题可能才刚刚开始...
jsonlite::fromJSON(substr(htext, 21, 5615711))
#> $stock
#> $stock$period_title
#> [1] "1T2018"
#>
#> $stock$total
#> [1] 4162848
#>
#> $stock$total_sale
#> [1] 3426559
#>
#> $stock$total_rental
#> [1] 736289
#>
#> $stock$total_es
#> [1] 2196851
#>
#> ... (very very long list)