Присвойте значение JsonArray переменной, соответствующей классу дел - PullRequest
1 голос
/ 09 января 2020

Я передаю и полезную нагрузку, которая похожа на case class reportdata, внутри класса. И мне нужно, чтобы получить значение report_data, которое является Option [JSArray],

Мне нужно назначить этот JSArray для переменной, если этот необязательный массив соответствует case class reportdata

case class Fields(
                      reportid: Option[Long],
                      report_data: Option[JsArray],
                      header :Option[JsArray],
                      footer :Option[JsArray]
                    )

case class reportdata(
                     customText : Option[String],
                     textAlignment: Option[String],
                     textSize : Option[Int],
                     pageHeight: Long,
                     pageWidth: Long
                     )

Json, который я передаю из БД, относится к типу полей класса Fields и имеет 3 JSON массива. поэтому я хочу выбрать массив json, соответствующий классу данных отчета, и я должен назначить его новой переменной.

"reports": [
        {
            "reportid":513,
            "report_data":[
                {
                    "formatType": "text",
                    "value": "This is a sample text to be printed in the report"
                },
                {
                    "formatType": "text size",
                    "value": 12
                },
                {
                    "formatType": "text alignment",
                    "value" : "RIGHT"
                },
                {
                    "formatType": "page height",
                    "value" : "12"
                },
                {
                    "formatType": "page width",
                    "value" : "8"
                }
            ],
            "header": [
                {
                    "formatType": "text",
                    "value": "Test"
                },
                {
                    "formatType": "font size",
                    "value": 12
                }
            ],
            "footer": [
                {
                    "formatType": "text",
                    "value": "Test"
                },
                {
                    "formatType": "font size",
                    "value": 12
                }
            ]
        }
    ]

1 Ответ

2 голосов
/ 26 февраля 2020

Использование Дижон FTW!

Вот тест, который демонстрирует, насколько легко можно найти "правильное" значение в образцах, подобных вашему:

    import com.github.pathikrit.dijon._

    val json = parse(
      """{
        |"data":[
        |  {
        |    "formatType": "text",
        |    "value": "bgyufcie huis huids hufhsduhfsl hd"
        |  },
        |  {
        |    "formatType": "text size",
        |    "value": 12
        |  },
        |  {
        |    "formatType": "text alignment",
        |    "value" : "right"
        |  }
        |]
        |}""".stripMargin)

assert(json.data.toSeq.collect { 
  case obj if obj.formatType == "text alignment" => obj.value 
}.head == "right")
...