问题
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