Scala: Parse JSON directly into a case class

后端 未结 7 2205
一生所求
一生所求 2020-12-24 11:36

Given a string of JSON, and a case class that corresponds to it, what\'s a simple way to parse the JSON into the case class? There are many libraries available, but it seem

7条回答
  •  梦毁少年i
    2020-12-24 12:03

    I've used https://github.com/json4s/json4s , only gripe so far is https://github.com/json4s/json4s/issues/137

    import org.json4s._
    import org.json4s.native.JsonMethods._
    
    implicit val formats = DefaultFormats
    
    case class ParsedPage(crawlDate: String, domain:String, url:String, text: String)
    
    val js = """ {
    "crawlDate": "20150226",
    "domain": "0x20.be",
    "url": "http://0x20.be/smw/index.php?title=99_Bottles_of_Beer&oldid=6214",
    "text": "99 Bottles of Beer From Whitespace (Hackerspace Gent) Revision as of 14:43, 8 August 2012 by Hans ( Talk | contribs ) 99 Bottles of Beer Where: Loading map... Just me, with 99 bottles of beer and some friends. Subpages"
    }"""
    
    
    parse(js).extract[ParsedPage]
    

提交回复
热议问题