R不显示来自json源的特殊字符 [英] R does not show special characters coming from json source

查看:109
本文介绍了R不显示来自json源的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关背景的一些信息. 我从提供公共交通数据的API中提取数据.它以json格式返回结果,我使用库"jsonlite"对其进行处理.

a little bit about the background. I pull data from an API that supplies public transportation data. It returns the result in json format, which I process with the library 'jsonlite'.

 resp <- GET(url = url)


  resp_char <- rawToChar(resp$content)
  parsed <- fromJSON(resp_char, flatten = T)

  parsed.df <- do.call(what = "rbind", args = lapply(parsed[1], as.data.frame))

问题在于,结果是没有特殊字符.

The problem is, in the result there are no special characters.

我正在Windows Server 2012计算机上工作,我在R中的语言设置如下所示:

I am working on a Windows Server 2012 machine and my language settings in R look like this:

    > Sys.getlocale()
[1] "LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252"

示例:

    > df$direction
"U Alt-Mariendorf (Berlin)" 
"U Alt-Tegel (Berlin)" 
"U Alt-Mariendorf (Berlin)"              
"U Alt-Tegel (Berlin)" 
"Märkisches Viertel, Wilhelmsruher Damm"

第五个结果的预期结果是威廉姆斯·达默尔(MärkischesViertel)"

The expected result for the fifth result is "Märkisches Viertel, Wilhelmsruher Damm"

之后,我查看了实际的编码.

After that I looked in the actual encoding.

> Encoding(df$direction)
   [1] "unknown" "unknown" "unknown" "unknown" "UTF-8"

我认为目前为止看起来不错,但是我看不到特殊字符.

In my opinion this looks good so far, but nevertheless I cannot see special characters.

我对此主题的任何建议和想法都很感谢.

I appreciate any suggestions and ideas on the subject.

致谢

推荐答案

所以最后我明白了. 感谢@parth,它使我找到了正确的答案. 我在fromJSON语句之前使用了Encoding,并且对我有用.

So finally I got it. Thanks to @parth, it has led me to the right answer. I used Encoding before my fromJSON statement and that worked for me.

  resp <- GET(url = url)

  resp_char <- rawToChar(resp$content)
  Encoding(resp_char) <- "UTF-8"
  parsed <- fromJSON(resp_char, flatten = T)

  parsed.df <- do.call(what = "rbind", args = lapply(parsed[1], as.data.frame))

这篇关于R不显示来自json源的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆