Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implementation ('com.beust:klaxon:3.0.1')
- [{
- "name" : "Vinoth",
- "age" : "25",
- "languages" : [
- "Tamil",
- "English",
- "Spanish"
- ]
- },
- {
- "name" : "varshth",
- "age" : "2",
- "languages" : [
- "Tamil",
- "English"
- ]
- }]
- package com.docker.kubetest
- import com.beust.klaxon.JsonReader
- import com.beust.klaxon.Klaxon
- import org.springframework.beans.factory.annotation.Autowired
- import org.springframework.boot.ApplicationArguments
- import org.springframework.boot.ApplicationRunner
- import org.springframework.core.io.ClassPathResource
- import org.springframework.stereotype.Component
- import java.io.StringReader
- @Component
- class TestAnnotation : ApplicationRunner {
- @Autowired
- private lateinit var readFile: ReadFile
- override fun run(args: ApplicationArguments?) {
- readFile.readFileDirectlyAsText()
- }
- }
- @Component
- class ReadFile {
- fun readFileDirectlyAsText() {
- val resource = ClassPathResource("myfile.json")
- val file = resource.file
- val readText = file.readText(Charsets.UTF_8)
- val klaxon = Klaxon()
- val personArray = arrayListOf<Person>()
- JsonReader(StringReader(readText)).use { reader ->
- reader.beginArray {
- while (reader.hasNext()) {
- val person = klaxon.parse<Person>(reader)
- personArray.add(person!!)
- }
- }
- }
- println(personArray)
- //Alternatively use the below line
- val parseArray = klaxon.parseArray<Person>(readText)
- println(parseArray)
- }
- }
- data class Person(val name: String, val age: String, val languages: List<String>)
Add Comment
Please, Sign In to add comment