将JsonArray值分配给与case类匹配的变量
我在类中传递与将JsonArray值分配给与case类匹配的变量,json,scala,case-class,Json,Scala,Case Class,我在类中传递与案例类reportdata类似的有效负载。我需要一个来获取report\u data的值,这是一个选项[JSArray] 如果可选数组与case class reportdata case class Fields( reportid: Option[Long], report_data: Option[JsArray], header :Option
案例类reportdata
类似的有效负载。我需要一个来获取report\u data
的值,这是一个选项[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
)
我从DB传递的Json是case类类型字段,它有3个Json数组。因此,我希望选择与报表数据的case类匹配的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
}
]
}
]
使用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")
请澄清你的问题。你试过什么/你用什么图书馆。