将 JsonArray 值分配给匹配大小写的变量 class
Assign JsonArray value to a variable matching case class
我在 class 中传递与 case class reportdata
类似的有效载荷。我需要一个来获取 report_data
的值,这是一个 Option[JSArray],
如果该可选数组与 case class reportdata
匹配,我需要将该 JSArray 分配给一个变量
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是caseclass类型的字段,它有3个JSON数组。所以我想要 select 匹配报告数据 class 大小写的 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
}
]
}
]
使用Dijon FTW!
这里有一个测试可以证明 "right" 值在像您这样的样本中是多么容易找到:
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")
我在 class 中传递与 case class reportdata
类似的有效载荷。我需要一个来获取 report_data
的值,这是一个 Option[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是caseclass类型的字段,它有3个JSON数组。所以我想要 select 匹配报告数据 class 大小写的 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
}
]
}
]
使用Dijon FTW!
这里有一个测试可以证明 "right" 值在像您这样的样本中是多么容易找到:
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")