R不显示来自json源的特殊字符

时间:2022-06-01 20:05:57

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'.

一点关于背景。我从提供公共交通数据的API中提取数据。它以json格式返回结果,我使用库'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:

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

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

第五个结果的预期结果是“MärkischesViertel,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

问候

2 个解决方案

#1


0  

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 <-data.frame(direction = c(“U Alt-Mariendorf(柏林)”,“U Alt-Tegel(柏林)”,“U Alt-Mariendorf(柏林)”,“U Alt-Tegel(柏林)” ,“MärkischesViertel,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

现在,只需将整个df $ direction列的编码更改为

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

#2


0  

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.

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

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

#1


0  

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 <-data.frame(direction = c(“U Alt-Mariendorf(柏林)”,“U Alt-Tegel(柏林)”,“U Alt-Mariendorf(柏林)”,“U Alt-Tegel(柏林)” ,“MärkischesViertel,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

现在,只需将整个df $ direction列的编码更改为

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

#2


0  

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.

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

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