Guest User

Untitled

a guest
Nov 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. library("rjson")
  2. fromJSON("[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]")
  3.  
  4. js <- '[{
  5. "id": 14,
  6. "name": "Fantasy"
  7. }, {
  8. "id": 28,
  9. "name": "Action"
  10. }, {
  11. "id": 53,
  12. "name": "Thriller"
  13. }]'
  14.  
  15. ## this is your invalid string
  16. js <- "[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]"
  17.  
  18. ## convert it to a valid string
  19. js <- gsub("QUUX", "'", gsub("'", '"', gsub('"', "QUUX", js)))
  20.  
  21. jsonlite::fromJSON(js)
  22.  
  23. # id name
  24. #1 18 Drama
  25. #2 28 Action
  26. #3 10749 Romance
  27.  
  28. rjson::fromJSON(js)
  29.  
  30. [[1]]
  31. [[1]]$id
  32. [1] 18
  33.  
  34. [[1]]$name
  35. [1] "Drama"
  36.  
  37.  
  38. [[2]]
  39. [[2]]$id
  40. [1] 28
  41.  
  42. [[2]]$name
  43. [1] "Action"
  44.  
  45.  
  46. [[3]]
  47. [[3]]$id
  48. [1] 10749
  49.  
  50. [[3]]$name
  51. [1] "Romance"
Add Comment
Please, Sign In to add comment