繁体   English   中英

将json字符串解析为scala对象

[英]parse json string to a scala object

我在scala中使用Play框架2.2.1版。

我有一个很大的json-string,我想从中构造一个对象。
我已经为类编写了格式化程序。

我的问题是我应该将该字符串转换为具体类的步骤是什么。

例如,我尝试过的代码:

val something = scala.util.parsing.json.JSON.parseFull(jsonContent).asInstanceOf[HsmTenant] //This has stucked my debug session and operation never completes

要么

val concreteClass = Json.parse(jsonContent).asInstanceOf[T] //message: "Error processing request - Failed to migrate, Exception=play.api.libs.json.JsObject cannot be cast to migration.hsm.HsmTenant"

尝试时:

Json.parse(jsonContent).as[T]

我得到以下内容:

 Multiple markers at this line
- No Json deserializer found for type migration.hsm.HsmTenant. Try to implement an implicit Reads or Format for this type.
- not enough arguments for method as: (implicit fjs: play.api.libs.json.Reads[migration.hsm.HsmTenant])migration.hsm.HsmTenant. Unspecified value 
 parameter fjs.
- No Json deserializer found for type migration.hsm.HsmTenant. Try to implement an implicit Reads or Format for this type.
- not enough arguments for method as: (implicit fjs: play.api.libs.json.Reads[migration.hsm.HsmTenant])migration.hsm.HsmTenant. Unspecified value 
 parameter fjs.
- Line breakpoint:HsmParser [line: 16] - parse(jsonContent: String): migration.hsm.HsmTenant

我的读写看起来像:他们编译:

trait HsmFormats {

implicit val hsmRetailerFormat = Json.format[Retailer]
implicit val hsmproductFormat = new Format[HsmProduct]
{
def writes(item: HsmProduct): JsValue = 
{
  val retailers = item.r.getOrElse(List[Retailer]())
  val images = item.images.getOrElse(List[String]())
  Json.obj(
    "u" -> item.u,
    "s" -> item.s,
    "z" -> item.z,
    "n" -> item.n,
    "v" -> item.v,
    "vu" -> item.vu,
    "t" -> item.t,
    "r" -> retailers,
    "images" -> images
  )
}

def reads(json: JsValue): JsResult[HsmProduct] = 
{
  val retailers = (json \ "r").as[Option[List[Retailer]]]
  var retaliersReal:Option[List[Retailer]] = None
  if (retailers.isDefined)
  {
      retaliersReal = Some(retailers.get)
  }

  val images = (json \ "images").as[Option[List[String]]]
  var imagesReal:Option[List[String]] = None
  if (images.isDefined)
  {
      imagesReal = Some(images.get)
  }      
JsSuccess(new HsmProduct(
  (json \ "u").as[String],
  (json \ "s").as[Int],
  (json \ "z").as[Int],
  (json \ "n").as[String],
  (json \ "v").as[String],
  (json \ "vu").as[String],
  (json \ "t").as[String],
  retailers,
  imagesReal
))
}

}

implicit val hsmTenantFormat = new Format[HsmTenant]
{
def writes(tenant: HsmTenant): JsValue = 
{
  val items = tenant.items.getOrElse(List[HsmProduct]())
  Json.obj(
    "items" -> tenant.items,
    "prefixAndroid" -> tenant.prefixAndroid,
    "prefixIOS" -> tenant.prefixIOS,
    "er" -> tenant.er,
    "erMessage" -> tenant.erMessage

  )
}

def reads(json: JsValue): JsResult[HsmTenant] = 
{
  val items = (json \ "items").as[Option[List[HsmProduct]]]
  var itemsReal:Option[List[HsmProduct]] = None
  if (items.isDefined)
  {
      itemsReal = Some(items.get)
  }  
JsSuccess(new HsmTenant(
  itemsReal,
  (json \ "prefixAndroid").as[String],
  (json \ "prefixIOS").as[String],
  (json \ "er").as[Int], 
  (json \ "erMessage").as[String]
))
}

}

如果您的Format是正确的,这应该可以工作:

import play.api.libs.json.Json

Json.parse(jsonContent).as[T]

有什么问题吗?
导入 Formats类不是问题,我的格式trait ,所以我必须在使用它的类中扩展该特性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM