• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    코틀린 json parsing with jackson
    https://github.com/FasterXML/jackson-module-kotlin
    
    1. app level build.gradle 파일내 dependencies 안에 compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.8.7" 추가 (버전은 변경될 수 있음)
    2. jackson instance
        val mapper = jacksonObjectMapper()
    
    3. json 데이터 예제 
    {
        "name": "DEMO",
        "version": 0,
        "server": {
            "url": "http://sarangnamu.net",
            "path": "/path"
        },
        "serverMap": {
            "map1": {
                "url": "http://map1.sarangnamu.net",
                "path": "/path"
            }
        }
    }
    
    4. data class 작성 
    data class Server(
        val name: String,
        val version: Int,
        val server: ServerInfo
        val serverMap: Map<String, ServerInfo>
    )
    
    data class ServerInfo(
        val url: String,
        val path: String
    )
    
    5. 파싱 
    val mapper = jacksonObjectMapper()
    val serverInfo = mapper.readValue<Server>(jsonString)
    val serverInfoString = mapper.writeValueAsString(serverInfo)
    
    끝.
    

    Comment


    입력하3 1508772377



    Locations of visitors to this page