将json字符串解析为scala对象

时间:2014-03-31 14:43:31

标签: json scala playframework playframework-2.2

我使用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]
))
}

}

2 个答案:

答案 0 :(得分:2)

如果您的Format是正确的,这应该有效:

import play.api.libs.json.Json

Json.parse(jsonContent).as[T]

答案 1 :(得分:0)

问题是什么?
如果导入 Formats类,我的格式特征,则问题,所以我必须扩展我使用它的类中的特征。