Свойство объекта JSON с пространством в Scala с использованием Json4s(Джексон)

#json #scala #api #json4s

#json #scala #API #json4s

Вопрос:

У меня есть строка JSON в приведенной ниже форме:

 var jStr = """ {  |"company":{  |"company name":"ABCD"  |},  |"person":[  |{"name":"john",  |"age":"28"  |},  |{  |"name":"AWQ",  |"age":"45"  |}  |]  |}  """.stripMargin  

Смотрите свойство «название компании». Из-за этого я не могу извлечь его из формы json в класс case, не изменив «название компании» на «имя_компании»(или что-либо еще). Ниже приведен код:

 import com.fasterxml.jackson.annotation.{JsonCreator, JsonProperty} import org.json4s._ import org.json4s.native.JsonMethods._   val parseJson = parse(jStr)  var obj = parseJson.extract[Info] // Exception Here   case class Company(cname : String) case class Info(company: Company,person: List[Person])  case class Person(name : String , age : String)  

Я попытался использовать @JsonProperty и @JsonCreator, но они тоже потерпели неудачу.

 @JsonCreator case class Company( @JsonProperty("company name") cname : String) case class Info(company: Company, person: List[Person])  case class Person(name : String , age : String)  

Мне нужно сопоставить свойство json, имеющее пробелы, с соответствующим классом case(в котором, очевидно, не может быть места!), используя Json4s в Scala.

Трассировка стека:

 Exception in thread "main" org.json4s.package$MappingException: No usable value for company No usable value for cname Did not find value which can be converted into java.lang.String  at org.json4s.reflect.package$.fail(package.scala:95)  at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$buildCtorArg(Extraction.scala:548)  at org.json4s.Extraction$ClassInstanceBuilder$anonfun$3.applyOrElse(Extraction.scala:572)  at org.json4s.Extraction$ClassInstanceBuilder$anonfun$3.applyOrElse(Extraction.scala:570)  at scala.PartialFunction.$anonfun$runWith$1$adapted(PartialFunction.scala:145)  at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)  at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)  at scala.collection.TraversableLike.collect(TraversableLike.scala:407)  at scala.collection.TraversableLike.collect$(TraversableLike.scala:405)  at scala.collection.AbstractTraversable.collect(Traversable.scala:108)  at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:570)  at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:630)  at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:416)  at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)  at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)  at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)  at scala.PartialFunction$anon$1.applyOrElse(PartialFunction.scala:257)  at org.json4s.Extraction$.customOrElse(Extraction.scala:637)  at org.json4s.Extraction$.extract(Extraction.scala:408)  at org.json4s.Extraction$.extract(Extraction.scala:40)  at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)  at JsonTest.convertToJSON(JsonTest.scala:100)  at Main$.main(Main.scala:8)  at Main.main(Main.scala) Caused by: org.json4s.package$MappingException: No usable value for cname Did not find value which can be converted into java.lang.String  at org.json4s.reflect.package$.fail(package.scala:95)  at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$buildCtorArg(Extraction.scala:548)  at org.json4s.Extraction$ClassInstanceBuilder$anonfun$3.applyOrElse(Extraction.scala:572)  at org.json4s.Extraction$ClassInstanceBuilder$anonfun$3.applyOrElse(Extraction.scala:570)  at scala.PartialFunction.$anonfun$runWith$1$adapted(PartialFunction.scala:145)  at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)  at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)  at scala.collection.TraversableLike.collect(TraversableLike.scala:407)  at scala.collection.TraversableLike.collect$(TraversableLike.scala:405)  at scala.collection.AbstractTraversable.collect(Traversable.scala:108)  at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:570)  at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:630)  at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:416)  at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)  at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)  at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)  at scala.PartialFunction$anon$1.applyOrElse(PartialFunction.scala:257)  at org.json4s.Extraction$.customOrElse(Extraction.scala:637)  at org.json4s.Extraction$.extract(Extraction.scala:408)  at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$buildCtorArg(Extraction.scala:534)  ... 23 more Caused by: org.json4s.package$MappingException: Did not find value which can be converted into java.lang.String  at org.json4s.reflect.package$.fail(package.scala:95)  at org.json4s.Extraction$.$anonfun$convert$2(Extraction.scala:735)  at scala.Option.getOrElse(Option.scala:189)  at org.json4s.Extraction$.convert(Extraction.scala:735)  at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:410)  at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)  at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)  at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)  at scala.PartialFunction$anon$1.applyOrElse(PartialFunction.scala:257)  at org.json4s.Extraction$.customOrElse(Extraction.scala:637)  at org.json4s.Extraction$.extract(Extraction.scala:408)  at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$buildCtorArg(Extraction.scala:534)  ... 42 more  Process finished with exit code 1  

Комментарии:

1. Вы пробовали case class Company(`company name`: String) ?

2. Это сработало. Я использовал обратные клавиши для имени переменной. (например, это` cname `: Строка ). Совершенно противоположное.