【问题标题】:parse json string to a scala object将 json 字符串解析为 scala 对象
【发布时间】:2014-03-31 14:43:31
【问题描述】:

我使用带有 scala 的 play framework 版本 2.2.1。

我有一个大的 json 字符串,我希望从中构造一个对象。
我已经为课程编写了格式化程序。

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

例如我尝试过的代码:

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]
))
}

}

【问题讨论】:

  • 您的格式可能超出范围?你导入了吗?
  • 当我尝试导入它们时:导入的 `HsmFormats' 被包 hsm 中的 trait HsmFormats 定义永久隐藏

标签: json scala playframework playframework-2.2


【解决方案1】:

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

import play.api.libs.json.Json

Json.parse(jsonContent).as[T]

【讨论】:

    【解决方案2】:

    出了什么问题?
    如果 import Formats 类,这不是问题,我的 formattrait,所以我必须 在我使用它的类中扩展该特征。

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2013-05-12
      • 2020-05-18
      相关资源
      最近更新 更多