简体   繁体   English

Scala Play JSON类型转换

[英]Scala Play JSON Type conversion

I'm currently struggling with the type conversion of the Play! 我目前正在为Play的类型转换而苦苦挣扎! Json library for Scala... 用于Scala的Json库...
I have data that looks like this: 我有看起来像这样的数据:

val myJson = Json.parse(...)

val mapping = Map[String, Option(JsValue)](
    "id" -> (myJson \ "Path" \ stringValue),
    "num" -> (myJson \ "Path" \ intValue),
    "precision" -> (myJson \ "Path" \ "floatValue")
)

Also, I have an Avro Schema that defines the data types of each field, in this case it's: 另外,我有一个Avro架构,用于定义每个字段的数据类型,在这种情况下,它是:
- id: String -id:字符串
- num: Int -num:整数
- precision: Float -精度:浮动

If I now iterate over the fields and check the types against the Avro Schema like this (Note that field in this case is the name of the value to get, eg "id"): 如果我现在遍历这些字段并按照如下所示的Avro Schema检查类型(请注意,在这种情况下,该字段是要获取的值的名称,例如“ id”):

    ...
    case Schema.Type.STRING => mapping.getOrElse(field, None) match {
      case Some(stringVal: JsValue) => stringVal.as[String]
      case None => null
    }
    case Schema.Type.INT => mapping.getOrElse(field, None) match {
      case Some(intVal) => intVal.as[Int]
      case None => null
    }
    case Schema.Type.FLOAT => mapping.getOrElse(field, None) match {
      case Some(floatVal) => floatVal.as[Float]
      case None => null
    }
    ...

I always get this Error: 我总是收到此错误:

[org.apache.kafka.streams.processor.internals.StreamThread]  - Streams application error during processing in thread [StreamThread-1]:play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(error.expected.jsnumber),WrappedArray())))))

(Note: of course the error.expected.jsnumber can also be jsstring sometimes) (注意:当然error.expected.jsnumber有时也可以是jsstring)

Here are the ways of conversion that I already tried, and did not work (I have several different of the mappings that in the end still contain either None or a value of given type): 这是我已经尝试但无法使用的转换方式(我有几种不同的映射,最后它们仍然包含None或给定类型的值):

floatVal.as[Float]
floatVal.as[JsNumber].as[Float]
floatVal.as[JsNumber].value.toFloat
Hi,

I'm currently struggling with the type conversion of the Play! 我目前正在为Play的类型转换而苦苦挣扎! Json library for Scala... 用于Scala的Json库...
I have data that looks like this: 我有看起来像这样的数据:

val myJson = Json.parse(...)

val mapping = Map[String, Option(JsValue)](
    "id" -> (myJson \ "Path" \ stringValue),
    "num" -> (myJson \ "Path" \ intValue),
    "precision" -> (myJson \ "Path" \ "floatValue")
)

Also, I have an Avro Schema that defines the data types of each field, in this case it's: 另外,我有一个Avro架构,用于定义每个字段的数据类型,在这种情况下,它是:
- id: String -id:字符串
- num: Int -num:整数
- precision: Float -精度:浮动

If I now iterate over the fields and check the types against the Avro Schema like this (Note that field in this case is the name of the value to get, eg "id"): 如果我现在遍历这些字段并按照如下所示的Avro Schema检查类型(请注意,在这种情况下,该字段是要获取的值的名称,例如“ id”):

    ...
    case Schema.Type.STRING => mapping.getOrElse(field, None) match {
      case Some(stringVal: JsValue) => stringVal.as[String]
      case None => null
    }
    case Schema.Type.INT => mapping.getOrElse(field, None) match {
      case Some(intVal) => intVal.as[Int]
      case None => null
    }
    case Schema.Type.FLOAT => mapping.getOrElse(field, None) match {
      case Some(floatVal) => floatVal.as[Float]
      case None => null
    }
    ...

I always get this Error: 我总是收到此错误:

[org.apache.kafka.streams.processor.internals.StreamThread]  - Streams application error during processing in thread [StreamThread-1]:play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(error.expected.jsnumber),WrappedArray())))))

(Note: of course the error.expected.jsnumber can also be jsstring sometimes) (注意:当然error.expected.jsnumber有时也可以是jsstring)

Here are the ways of conversion that I already tried, and did not work (I have several different of the mappings that in the end still contain either None or a value of given type): 这是我已经尝试但无法使用的转换方式(我有几种不同的映射,最后它们仍然包含None或给定类型的值):

floatVal.as[Float]
floatVal.as[JsNumber].as[Float]
floatVal.as[JsNumber].value.toFloat
floatVal.as[String].value.toFloat
floatVal.as[JsString].value.toString.toFloat
floatVal.as[String].toFloat
floatVal.as[String].value.toFloat
floatVal.as[JsString].value.toString.toFloat
floatVal.as[JsString].as[String].toFloat
floatVal.as[JsString].as[String].value.toFloat
floatVal.as[JsString].as[String].value.toString.toFloat

Somehow I have problems for all the types, only in some cases, but I have them... 不知何故,仅在某些情况下,我对所有类型都有问题,但是我遇到了问题...
What is also interesting is, that even though the type is JsValue , when I look at them with the Debugger, the type says Js<type> , eg JsString instead of JsValue . 有趣的是,即使类型是JsValue ,当我在调试器中查看它们时,该类型JsString Js<type> ,例如JsString而不是JsValue
In some occasions, however not always, it also displays JsString for the Int type... 在某些情况下,但并非总是如此,它还会为Int类型显示JsString ...

Why doesn't it keep them as JsValue ? 为什么不将它们保留为JsValue

What is the right way to convert the Play! 转换Play的正确方法是什么! Json types? Json类型?

Thank you All the best Tim 谢谢蒂姆

just making sure to update with the way that worked for me. 只是确保以适合我的方式进行更新。

In my case I just did the JsString , JsNumber , ... conversion right away on my own so that the framework is not inferring the type of it on it's own. 在我的情况下,我只是自己进行了JsStringJsNumber ,...转换,因此框架不会自行推断其类型。

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

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