Scala 将JsArray的JsArray转换为Case类
我的问题是一种延伸,也是一种延伸 我的JSON看起来像:Scala 将JsArray的JsArray转换为Case类,scala,playframework,jsvalue,Scala,Playframework,Jsvalue,我的问题是一种延伸,也是一种延伸 我的JSON看起来像: { "id": "id1", "results": [ { "exceed_size": "yes", "rows_count": 1001, "runtime_seconds": 0.02199999988079071, "columns": [ "COL_1",
{
"id": "id1",
"results": [
{
"exceed_size": "yes",
"rows_count": 1001,
"runtime_seconds": 0.02199999988079071,
"columns": [
"COL_1",
"COL_2",
"COL_3",
"COL_4",
"COL_5",
"COL_6",
"COL_7",
"COL_8",
"COL_9"
],
"columns_type": [
"number",
"string",
"string",
"string",
"number",
"time",
"time",
"number",
"string"
],
"limit": 1000,
"index": 0,
"rows": [
[
"9",
" C68894",
" ",
"",
"0",
"2018-05-02 03:13:00.0",
"2017-12-02 22:32:00.0",
"",
"Approved "
],
[
"65",
"325806 ",
"msm ",
" ",
"2",
"2018-05-02 03:13:00.0",
"2018-07-06 06:00:00.0",
"13",
"Approved "
],
...
]
},
...
]
}
我正在使用Play框架提供的JSON库进行JSON解析
如果查看行
值,它是字符串值的JsArray
的JsArray
。我一直在尝试将行
转换为case类的对象列表,我的case类如下所示:
case类行(列1:String,列2:String,…,列9:String)
我试着做一些类似的事情:
val rows = (response \\ "rows").head.as[List[List[(String, String, ... , String)]]].flatten
尝试这种方法会产生一个错误,我确信这是行不通的。如何将这样的JsArray
转换为case类的对象列表
编辑1:
正如@MilanRegmi所建议的,我试着:
implicit val jsonFormat: Format[Rows] = Json.format[Rows]
val emails = (response \ "results" \ "rows").as[JsArray]
.value.map(j => j.validate[Rows].get)
尝试这样做的结果是:
Exception in thread "main" play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List([{"exceed_size":"yes","rows_count":1001,"runtime_seconds":0.01600000075995922,"columns":["COL_1","COL_2","COL_3","COL_4","COL_5","COL_6","COL_7","COL_8","COL_9"],"columns_type":["number","string","string","string","number","time","time","number","string"],"limit":1000,"index":0,"rows":[["9"," C68894","","","0","2018-05-02 03:13:00.0","2017-12-02 22:32:00.0","","Approved "],["65","325806 ","msm "," ","2","2018-05-02 03:13:00.0","2018-07-06 06:00:00.0","13","Approved "],...]}] is not an object),WrappedArray())))))
at play.api.libs.json.JsReadable$$anonfun$2.apply(JsReadable.scala:25)
at play.api.libs.json.JsReadable$$anonfun$2.apply(JsReadable.scala:25)
at play.api.libs.json.JsError.fold(JsResult.scala:64)
at play.api.libs.json.JsReadable$class.as(JsReadable.scala:23)
at play.api.libs.json.JsUndefined.as(JsLookup.scala:181)
at com.cmdwldap.restapi.User.getEntitlementUserData(User.scala:150)
at com.cmdwldap.restapi.User$.main(User.scala:168)
at com.cmdwldap.restapi.User.main(User.scala)
注:第150行对应于提到
val电子邮件的位置。试试这个:
val rows: Seq[Rows] = (json \ "result" \ "rows").as[JsArray].value.map(j => j.validate[Rows].get)
更新:
val rows: Seq[Rows] = (json \ "result" \ "rows").as[JsArray].value.map(j => j.validate[Rows].get)
把你的问题看了好几遍之后。我现在有你的问题了。您想将List[List[String]
转换为CaseClass
首先,不能将字符串列表的数组直接转换为case类,所以需要转换Array[String]
toJsObject
其中key
应该是类的fieldName
。之后,我们可以使用反射获取fieldName
。然后,我们需要创建JsObject
以匹配caseClass
和使用fieldName
列出[字符串]
从语法上讲,上述场景可以通过以下方式解决:
case class Rows(col1: Option[String] = None,
col2: Option[String] = None,
col3: Option[String] = None,
col4: Option[String] = None,
col5: Option[String] = None,
col6: Option[String] = None,
col7: Option[String] = None,
col8: Option[String] = None,
col9: Option[String] = None)
implicit val reads=Json.reads[Rows]
这是我们的case类,隐式读取
导入scala.reflect.runtime.universe.\ucode>
def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
case m: MethodSymbol if m.isCaseAccessor => m
}.toList
val rowFieldNames = classAccessors[Rows].map(k => k.name.toString)
val results = (json \ "results").as[JsArray].value.flatMap{
r => (r \ "rows").as[JsArray].value
}.map{row =>
val rowArray = row.as[JsArray]
val rowArraySeq = rowArray.value.map(_.as[JsString]).map(_.value)
val map = rowArraySeq.foldLeft(Map.empty[String, JsValue]){
(r, c) =>
val indexOfCurrentValue = rowArraySeq.indexOf(c)
val fieldName = rowFieldNames(indexOfCurrentValue)
r.+((fieldName, JsString(c)))
}
val rowJsObject = JsObject(map)
Json.toJson(rowJsObject)
}.toList
错误:找不到参数rds:Reads[Rows]
的隐式读写操作。请添加如下隐式格式化程序:implicit val jsonFormat:Format[Rows]=Json.Format[Rows]请检查编辑1您似乎提供了无效的json。请首先检查您的json并确保它是有效的json。它是有效的json。:(您正在寻找类似的内容吗?(json.parse(jsonString)\“results”\0\“rows”)。验证[列表[列表[字符串]]