【问题标题】:Read from JSON where URL contains funny character从 URL 包含有趣字符的 JSON 中读取
【发布时间】:2020-11-20 12:35:53
【问题描述】:

我正在尝试从其中 URL 包含 R 中的非标准拉丁字符的 API 读取,但出现错误。没有有趣字符的类似 URL 可以正常工作。

我收到以下错误

> RJSONIO::fromJSON("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbappé")
Error in file(con, "r") : cannot open the connection

标准字符可以正常工作

res <- RJSONIO::fromJSON("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/neymar")

【问题讨论】:

    标签: r json urlencode rjsonio


    【解决方案1】:

    这更像是一个 url 编码问题,而不是 R 或 JSON 问题。 但这应该可行:

    RJSONIO::fromJSON("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbapp%C3%A9")
    

    这个link 也可以帮助您获得理论背景信息,这个one 也可以帮助您进行实际转换。

    您也可以使用utils::URLencode 在 R 中进行转换:

    utils::URLencode("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbappé")
    [1] "https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbapp%C3%A9"
    

    【讨论】:

    • 非常感谢,但是知道我为什么会得到这个吗? &gt; URLencode('é') [1] "%E9" 而不是像你这样的 %C3%A9?大概是一个设置
    • 取决于字符集:hhttps://www.w3schools.com/tags/ref_urlencode.asp Windows-1252 UTF-8
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多