R does not show special characters coming from json source

孤者浪人 提交于 2019-12-02 06:21:02

问题


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.

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"

Example:

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

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.

Regards


回答1:


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))



回答2:


Using the dataframe

df<-data.frame(direction=c("U Alt-Mariendorf (Berlin)","U Alt-Tegel (Berlin)","U Alt-Mariendorf (Berlin)","U Alt-Tegel (Berlin)","Märkisches Viertel, Wilhelmsruher Damm"), stringsAsFactors = FALSE)

> df
                                direction
1               U Alt-Mariendorf (Berlin)
2                    U Alt-Tegel (Berlin)
3               U Alt-Mariendorf (Berlin)
4                    U Alt-Tegel (Berlin)
5 Märkisches Viertel, Wilhelmsruher Damm

Now, just change the encoding of entire df$direction column as

Encoding(df$direction) <- "UTF-8"

which fixes the issue

> df
                               direction
1              U Alt-Mariendorf (Berlin)
2                   U Alt-Tegel (Berlin)
3              U Alt-Mariendorf (Berlin)
4                   U Alt-Tegel (Berlin)
5 Märkisches Viertel, Wilhelmsruher Damm


来源:https://stackoverflow.com/questions/46114449/r-does-not-show-special-characters-coming-from-json-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!