热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PlayFrameworkJson中的动态Json值-DynamicJsonvaluesinPlayFrameworkJson

IamcurrentlyusingthePlayframeworkjsonparserinordertoparseajsonstringinmyscalacode

I am currently using the Play framework json parser in order to parse a json string in my scala code.

我目前正在使用Play框架json解析器来解析我的scala代码中的json字符串。

I have the following class:

我有以下课程:

case class Address(address: String,
                   gps: GPS,
                   country: String) {}

object Address {    
  implicit val reads: Reads[Address] = (
      (JsPath \ "address").read[String] and
      (JsPath \ "gps").read[GPS] and
      (JsPath \ "country").read[String]
    ) (Address.apply _)

  implicit val writes: Writes[Address] = (
      (JsPath \ "address").write[String] and
      (JsPath \ "gps").write[GPS] and
      (JsPath \ "country").write[String]
    ) (unlift(Address.unapply))
}

Which works fine with the following json:

哪个适用于以下json:

{
    "address": "123 Fake Street",
    "country": "USA",
    "gps": { ... }
}

The problem is that in some situations the json may instead have the gps field be a string which doesnt parse, i.e.

问题是在某些情况下,json可能会将gps字段改为不解析的字符串,即

{
    "address": "123 Fake Street",
    "country": "USA",
    "gps": "123abc"
}

Now I know that I cant have the gps member be both a string or a GPS object, but is there any way to have it be say an Option[GPS] and only have a value if the json contained a gps object?

现在我知道我不能让gps成员同时成为一个字符串或一个GPS对象,但有没有办法让它说一个选项[GPS]并且只有一个值,如果json包含一个gps对象?

1 个解决方案

#1


2  

Only very little is needed to be changed in your impl. You need to read the field "gps" as something that is 'safe' like JsValue and then try to map it into your GPS case class if it can be done, if not, return None.

只需要很少的东西就可以在你的impl中改变。您需要将“gps”字段读作“JsValue”之类的“安全”字段,然后尝试将其映射到GPS案例类中,如果可以,则返回“无”。

case class GPS(a:String, b:String)
object GPS {
  val travelInfoReads = Json.reads[GPS]
  val travelInfoWrites = Json.writes[GPS]
  implicit val travelInfoFormat: Format[GPS] = Format(travelInfoReads, travelInfoWrites)
}
case class Address(address: String,
                   gps: Option[GPS],
                   country: String) {}

object Address {
  implicit val reads: Reads[Address] = (
    (JsPath \ "address").read[String] and
      (JsPath \ "gps").read[JsValue].map(js => js.asOpt[GPS]) and
      (JsPath \ "country").read[String]
    ) (Address.apply _)

  implicit val writes: Writes[Address] = (
    (JsPath \ "address").write[String] and
      (JsPath \ "gps").writeNullable[GPS] and
      (JsPath \ "country").write[String]
    ) (unlift(Address.unapply))
}

I also tested it:

我也测试了它:

val json = Json.toJson(Address("1",Some(GPS("a","b")),"2"))
      println(json)
      println(json.as[Address])
      val newObj: JsObject = (json.as[JsObject] - "gps") + ("gps" -> JsNumber(1))
      println(newObj)
      val a = newObj.as[Address]
      println(a)
      a must beEqualTo(Address("1",None,"2"))

Output was like

输出就像

{"address":"1","gps":{"a":"a","b":"b"},"country":"2"}
Address(1,Some(GPS(a,b)),2)
{"address":"1","country":"2","gps":1}
Address(1,None,2)

推荐阅读
  • 1、创建高级对象使用构造函数来创建对象构造函数是一个函数,调用它来例示并初始化特殊类型的对象。可以使用new关键字来调用一个构造函数。下面给出了使用构造函数的新示例。 ... [详细]
  • 导读:今天编程笔记来给各位分享关于php变量命名规范是什么的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: ... [详细]
  • C#设计模式(8)——桥接模式(Bridge Pattern)
    原文地址:http:www.cnblogs.comzhilipBridgePattern.html原文作者:Learninghard原文出处:博客园一、引言 ... [详细]
  • 这篇文章主要讲解了“GradeBook类怎么定义”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Grad ... [详细]
  • C#使用System.Net.Mail类实现邮件发送【.Net开发】
    这篇文章介绍了C#使用System.Net.Mail类实现邮件发送的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值, ... [详细]
  • Proof (of knowledge) of exponentiation
    1.ProofofexponentiationProofofexponentiation是基于adaptiverootassumption(充分必要条件࿰ ... [详细]
  • 883.三维形体投影面积
    题目883.三维形体投影面积题目大意在nxn的网格grid中,我们放置了一些与x,y,z三轴对齐的1x1x1立方体。每个值vgri ... [详细]
  • The“travellingsalesmanproblem”asksthefollowingquestion:“Givenalistofcitiesandthedistancesb ... [详细]
  • Qt 学习笔记(5)绘图   五子棋游戏
    在上一篇博客CQt学习笔记(4)绘图中介绍了Qt中的绘图方法,基于上一篇的博客的知识,使用QPainter设计一个五子棋的棋 ... [详细]
  • 【go密码学】对称加密算法
    对称加密对称加密算法是相对于非对称加密算法而言,两者的区别在于,对称加密和加密和解密时使用相同的秘钥,而非对称加密在加密和解密时使用不同的秘钥(公钥和私钥)。常见的对称加密算法:D ... [详细]
  • String字符串java.lang;基本标识Java字符串的一个重要特点就是字符串不可变。finalclassString没有子类字符串字面量也是一个String类的实例存储在字 ... [详细]
  • 关于初学PHP时的知识积累总结【PHP】
    后端开发|php教程PHP,知识积累后端开发-php教程PHP基础A、初识PHPPHP是与HTML混合使用的嵌入式语言。1、PHP标记默认标记短标记,需在php.ini中将shor ... [详细]
  • 2019 年 Firebase 峰会上发布的新功能
    作者FrancisMa,HeadofProductFirebase的使命是帮助移动开发者和Web开发者迈向成功,但考虑到Firebase每个月有超过200万个活跃的应 ... [详细]
  • 1.方法一:采用OleDB读取EXCEL文件:把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下:publicDa ... [详细]
  • 记录工作和学习中遇到和使用过的Python库。Target四个Level整理Collect学习Learn练习Practice掌握Master1.Python原生和功能增强1.1py ... [详细]
author-avatar
bv方法_484
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有