2

I try to convert a json object to R dataframe, here is the json object:

json <-
    '[
{"Name" : "a", "Age" : 32, "Occupation" : "凡达"}, 
{"Name" : "b", "Age" : 21, "Occupation" : "打蜡设计费"},
{"Name" : "c", "Age" : 20, "Occupation" : "的拉斯克奖飞"}
]'

then I use fromJSON, mydf <- jsonlite::fromJSON(json), the result is

  Name Age                                       Occupation
1    a  32                                 <U+51E1><U+8FBE>
2    b  21         <U+6253><U+8721><U+8BBE><U+8BA1><U+8D39>
3    c  20 <U+7684><U+62C9><U+65AF><U+514B><U+5956><U+98DE>

I was wondering how this happens, and is there any solution?

Using the package rjson can solve the problem, but the output is a list, but I want a dataframe output.

Thank you.

I've tried Sys.setlocale(locale = "Chinese"), well the characters are indeed Chinese,but the results are still weird like below:

Name Age Occupation 1 a 32 ·²´ï 2 b 21 ´òÀ¯Éè¼Æ·Ñ 3 c 20 µÄÀ­Ë¹¿Ë½±·É

rankthefirst
  • 1,370
  • 2
  • 14
  • 26
  • Related [post](http://stackoverflow.com/questions/26519455/error-parsing-json-file-with-the-jsonlite-package) – akrun Sep 25 '16 at 18:14
  • Those symbols in the output dataframe *are* an accurate representation of your inputs, they are simply printed that way in unicode. This has little to do with `fromJSON`. For instance, you can see the characters properly if you do `mydf$Occupation`. What are you using this for? – Chrisss Sep 25 '16 at 19:09
  • Your code produces the data frame with the correct characters in the `Occupation` column for me. What are you using to run R, RGUI, RStudio? Also, what's in `Sys.getlocale()` ? – SymbolixAU Sep 25 '16 at 23:43
  • @Chrisss That's right! So when I'm actually using the dataframe, that's OK, the problem only exists when the dataframe is printed? But in my case, I need to see the printed results to proceed the next step, – rankthefirst Sep 26 '16 at 02:36
  • Why do you need to see the printed results? Sorry, if I'm asking more than answering, but this is simply how characters are encoded. You can even do `str(json)` and see that even in your raw string input, such encoding exists. Remember, you can still *use* the chinese characters. For example `mydf[mydf$Occupation == "的拉斯克奖飞", ]` – Chrisss Sep 26 '16 at 03:05
  • @Chrisss, In the first stage, I have to see the name, age and occupation at the same time. In this stage, it's only a look at the output, then start the second stage. So if I only do `mydf$Occupation` I can see the Chinese characters, but I won't know the corresponding name and age. – rankthefirst Sep 26 '16 at 05:55
  • I see. Then `print(as.matrix(mydf))` should do just fine in the interim. – Chrisss Sep 26 '16 at 08:32
  • @Chrisss Well, it seems I've to use this as a solution, thanks~ – rankthefirst Sep 26 '16 at 11:58

0 Answers0