在Json4s中使用案例类将(非常)复杂的JSON对象转换为Scala对象 [英] Converting (Very) Complicated JSON objects to Scala Objects With Case Classes In Json4s

查看:372
本文介绍了在Json4s中使用案例类将(非常)复杂的JSON对象转换为Scala对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的JSON文件,如下所示:

I have a very complicated JSON file that looks like this:

       {
            "Animals": [
                [
                    100,
                    "Mammals",
                    [
                        1,
                        "Cat",
                        50,
                        45,
                        57,
                        -1
                    ],
                    [
                        2,
                        "Dog",
                        31,
                        44,
                        18,
                        -1
                    ]
                ],
    [
                159,
                "Reptiles",
                [
                    1,
                    "Lizard",
                    11,
                    12,
                    9,
                    -1
                ]
            ]
]
    }

我正在尝试解析此结构,并以某种方式从中获取scala对象.

I am attempting to parse this structure and somehow get scala objects out of it.

这是我的尝试:

case class Facts(number: Int, subTypeOfAnimal: String, data: List[Int])

case class Animaltype(value: Int, typeOfAnimal: String, characteristics: List[Facts])

case class Animal(rows: List[Animaltype])

这当然不能转换数据.它返回一个JNothing.我想知道如何在这种JArrays中正确表达复杂的JArray.

This, of course, fails to convert the data. It returns a JNothing. I am wondering how I can express complex JArrays within JArrays of this kind properly.

任何帮助都是有用的

谢谢!

推荐答案

您可以为FactsAnimalType定义CustomSerializer.

import scala.util.Try

import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization

case class Facts(number: Int, subTypeOfAnimal: String, data: List[Int])
case class AnimalType(value: Int, typeOfAnimal: String, characteristics: List[Facts])
case class Animal(Animals: List[AnimalType])

implicit val formats = Serialization.formats(NoTypeHints) + 
  new AnimalTypeSerializer + new FactsSerializer

class FactsSerializer extends CustomSerializer[Facts](format => ( {
  case JArray(JInt(nr) :: JString(subType) :: data) => 
    Facts(nr.toInt, subType, data.collect{ case JInt(i) => i.toInt})
}, { case _ => throw new RuntimeException("No serializing")}))

class AnimalTypeSerializer extends CustomSerializer[AnimalType](format => ( {
  case JArray(JInt(value) :: JString(typeAnimal) :: factsArrays) => 
    val facts = factsArrays.collect { case facts: JArray => 
      Try(facts.extract[Facts]).toOption
    }.flatten
    AnimalType(value.toInt, typeAnimal, facts)
}, { case _ => throw new RuntimeException("No serializing")}))

如果将输入json作为值json,则可以使用:

If you take your input json as the value json you can deserialize it with :

parse(json).extract[Animal]
// Animal = Animal(List(
//   AnimalType(100,Mammals,List(
//     Facts(1,Cat,List(50, 45, 57, -1)), Facts(2,Dog,List(31, 44, 18, -1))
//   )),
//   AnimalType(159,Reptiles,List(
//     Facts(1,Lizard,List(11, 12, 9, -1))
//   ))
// ))

这篇关于在Json4s中使用案例类将(非常)复杂的JSON对象转换为Scala对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆