scala Play 框架 - 向 JSON 对象添加字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22855710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:10:22  来源:igfitidea点击:

Play Framework - add a field to JSON object

jsonscalaplayframeworkplayframework-2.0

提问by Pawe? Kozikowski

I have a problem with adding a field to Json object in Play Framework using Scala:

我在使用 Scala 向 Play Framework 中的 Json 对象添加字段时遇到问题:

I have a case class containing data. For example:

我有一个包含数据的案例类。例如:

case class ClassA(a:Int,b:Int)

and I am able to create a Json object using Json Writes:

我可以使用 Json Writes 创建一个 Json 对象:

val classAObject = ClassA(1,2)
implicit val classAWrites= Json.writes[ClassA]
val jsonObject = Json.toJson(classAObject)

and the Json would look like:

和 Json 看起来像:

{ a:1, b:2 }

Let's suppose I would like to add an additional 'c' field to the Json object. Result:

假设我想向 Json 对象添加一个额外的“c”字段。结果:

{ a:1, b:2, c:3 }

How do I do that without creating a new case class or creating my Json object myself using Json.obj? I am looking for something like:

如何在不创建新案例类或使用 Json.obj 自己创建 Json 对象的情况下做到这一点?我正在寻找类似的东西:

jsonObject.merge({c:3}) 

Any help appreciated!

任何帮助表示赞赏!

回答by Travis Brown

JsObjecthas a +method that allows you to add fields to an object, but unfortunately your jsonObjectis statically typed as a JsValue, not a JsObject. You can get around this in a couple of ways. The first is to use as:

JsObject有一个+方法可以让您向对象添加字段,但不幸的jsonObject是,您的静态类型为 a JsValue,而不是 a JsObject。您可以通过多种方式解决此问题。第一种是使用as

 scala> jsonObject.as[JsObject] + ("c" -> Json.toJson(3))
 res0: play.api.libs.json.JsObject = {"a":1,"b":2,"c":3}

With asyou're essentially downcasting—you're telling the compiler, "you only know that this is a JsValue, but believe me, it's also a JsObject". This is safe in this case, but it's not a good idea. A more principled approach is to use the OWritesdirectly:

随着as你基本上向下转换-你告诉编译器,“你只知道这是一个JsValue,但相信我,这也是一个JsObject”。在这种情况下这是安全的,但这不是一个好主意。一个更有原则的方法是OWrites直接使用:

scala> val jsonObject = classAWrites.writes(classAObject)
jsonObject: play.api.libs.json.JsObject = {"a":1,"b":2}

scala> jsonObject + ("c" -> Json.toJson(3))
res1: play.api.libs.json.JsObject = {"a":1,"b":2,"c":3}

Maybe someday the Jsonobject will have a toJsonObjectmethod that will require a OWritesinstance and this overly explicit approach won't be necessary.

也许有一天该Json对象会有一个toJsonObject需要OWrites实例的方法,而这种过于明确的方法将是不必要的。

回答by Pawe? Kozikowski

I found a solution myself. In fact the JsValue, which is the return type of Json.toJson has no such method, but the JsObject (http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.libs.json.JsObject) does, so the solution is:

我自己找到了解决方案。实际上,Json.toJson 的返回类型 JsValue 没有这样的方法,但是 JsObject ( http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api .libs.json.JsObject) 确实如此,所以解决方案是:

val jsonObject = Json.toJson(classAObject).as[JsObject]
jsonObject + ("c", JsNumber(3)) 

I hope someone will find this useful :)

我希望有人会发现这很有用:)

回答by Vishnu Kumar

simpler way is to use argoanut (http://argonaut.io/)

更简单的方法是使用 argoanut ( http://argonaut.io/)

var jField : Json.JsonField = "myfield" //Json.JsonField is of type String
obj1.asJson.->:(jField, obj2.asJson)  // adds a field to obj1.asJson

here obj1.asJsoncreates a JSON object and obj2is the object to be added to the json created by obj1.asJson

这里obj1.asJson创建了一个 JSON 对象,并且obj2是要添加到由创建的 json 中的对象obj1.asJson