使用特殊字符喷洒 json 属性

spray json attribute with special char

我有 json,其中属性名称有一个特殊字符。试图让它用 spray json 解析。下面是代码 如何将 json @xml:lang 中的属性名称解析为大小写 class.

import spray.json._
import DefaultJsonProtocol._

object SpecialCharInName extends App {

  case class Person(name: String, `@xml:lang`: String)

  val json = """ {"name":"MyName", "@xml:lang":"US"} """

  object PersonProtocol extends DefaultJsonProtocol {
    implicit val personFormat = jsonFormat2(Person)
  }

  import PersonProtocol._
  val person = json.parseJson
  val personClass = person.convertTo[Person]

  println(personClass)

}

上面的代码抛出如下异常

[error] (run-main-0) spray.json.DeserializationException: Object is missing required member '@xml$colonlang'
spray.json.DeserializationException: Object is missing required member '@xml$colonlang'
at spray.json.package$.deserializationError(package.scala:23)
at spray.json.ProductFormats$class.fromField(ProductFormats.scala:60)
at c.c.s.f.v.d.SpecialCharInName$PersonProtocol$.fromField(SpecialCharInName.scala:12)
at spray.json.ProductFormatsInstances$$anon.read(ProductFormatsInstances.scala:56)
at spray.json.ProductFormatsInstances$$anon.read(ProductFormatsInstances.scala:46)
at spray.json.JsValue.convertTo(JsValue.scala:31)
at c.c.s.f.v.d.SpecialCharInName$.delayedEndpoint$com$comcast$sv$fabric$vimond$domain$SpecialCharInName(SpecialCharInName.scala:20)
at c.c.s.f.v.d.SpecialCharInName$delayedInit$body.apply(SpecialCharInName.scala:6)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at c.c.s.f.v.d.SpecialCharInName$.main(SpecialCharInName.scala:6)
at c.c.s.f.v.d.SpecialCharInName.main(SpecialCharInName.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
Caused by: java.util.NoSuchElementException: key not found: @xml$colonlang
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:59)
at spray.json.ProductFormats$class.fromField(ProductFormats.scala:57)
at c.c.s.f.v.d.SpecialCharInName$PersonProtocol$.fromField(SpecialCharInName.scala:12)
at spray.json.ProductFormatsInstances$$anon.read(ProductFormatsInstances.scala:56)
at spray.json.ProductFormatsInstances$$anon.read(ProductFormatsInstances.scala:46)
at spray.json.JsValue.convertTo(JsValue.scala:31)
at c.c.s.f.v.d.SpecialCharInName$.delayedEndpoint$com$comcast$sv$fabric$vimond$domain$SpecialCharInName(SpecialCharInName.scala:20)
at c.c.s.f.v.d.SpecialCharInName$delayedInit$body.apply(SpecialCharInName.scala:6)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at c.c.s.f.v.d.SpecialCharInName$.main(SpecialCharInName.scala:6)
at c.c.s.f.v.d.SpecialCharInName.main(SpecialCharInName.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)

提前感谢您的帮助。

萨阿德

import spray.json._
import DefaultJsonProtocol._

object SpecialCharInName extends App {

  case class Person(name: String, `@xml:lang`: String)

  val json = """ {"name":"MyName", "@xml:lang":"US"} """

  object PersonProtocol extends DefaultJsonProtocol {
    implicit val personFormat = jsonFormat(Person.apply, "name", "@xml:lang")
  }

  import PersonProtocol._

  val person = json.parseJson
  val personClass = person.convertTo[Person]
  println(personClass)
}

尽情享受吧:)

简单地说,使用接受构建器方法(如apply)的专用 jsonFormat 方法和必须出现在正在解析的 JSON 文档中的命名属性列表。默认的在你的情况下效果不佳。