Guest User

Untitled

a guest
Jul 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. implementation ('com.beust:klaxon:3.0.1')
  2.  
  3. [{
  4. "name" : "Vinoth",
  5. "age" : "25",
  6. "languages" : [
  7. "Tamil",
  8. "English",
  9. "Spanish"
  10. ]
  11. },
  12. {
  13. "name" : "varshth",
  14. "age" : "2",
  15. "languages" : [
  16. "Tamil",
  17. "English"
  18. ]
  19. }]
  20.  
  21.  
  22. package com.docker.kubetest
  23.  
  24. import com.beust.klaxon.JsonReader
  25. import com.beust.klaxon.Klaxon
  26. import org.springframework.beans.factory.annotation.Autowired
  27. import org.springframework.boot.ApplicationArguments
  28. import org.springframework.boot.ApplicationRunner
  29. import org.springframework.core.io.ClassPathResource
  30. import org.springframework.stereotype.Component
  31. import java.io.StringReader
  32.  
  33. @Component
  34. class TestAnnotation : ApplicationRunner {
  35.  
  36. @Autowired
  37. private lateinit var readFile: ReadFile
  38.  
  39. override fun run(args: ApplicationArguments?) {
  40. readFile.readFileDirectlyAsText()
  41. }
  42.  
  43. }
  44.  
  45. @Component
  46. class ReadFile {
  47.  
  48. fun readFileDirectlyAsText() {
  49.  
  50. val resource = ClassPathResource("myfile.json")
  51. val file = resource.file
  52. val readText = file.readText(Charsets.UTF_8)
  53. val klaxon = Klaxon()
  54. val personArray = arrayListOf<Person>()
  55. JsonReader(StringReader(readText)).use { reader ->
  56. reader.beginArray {
  57. while (reader.hasNext()) {
  58. val person = klaxon.parse<Person>(reader)
  59. personArray.add(person!!)
  60. }
  61. }
  62. }
  63.  
  64. println(personArray)
  65. //Alternatively use the below line
  66. val parseArray = klaxon.parseArray<Person>(readText)
  67. println(parseArray)
  68.  
  69. }
  70. }
  71.  
  72. data class Person(val name: String, val age: String, val languages: List<String>)
Add Comment
Please, Sign In to add comment