当前位置: 首页 > 知识库问答 >
问题:

Scala/Play:无法识别的标记“non”:应为“null”、“true”、“false”或NaN

尹超
2023-03-14

我有一个Scala模型

case class Model(
  name:          String,
  site:          String,
  phones:        Seq[String],
  duration:      String,
  pricePerMonth: Double,
  address:       String,
  benefits:      Seq[String],
  rating:        Int
)
{
  "name": "a",
  "site": "b",
  "phones": ["c"],
  "duration": "d",
  "price-per-month": 10.00,
  "address": "e",
  "benefits": ["f"],
  "rating": 1
}
"com.typesafe.play" %% "play-json" % "2.3.4"
val model = fromInputStream { getClass getResourceAsStream "/model.json"
                            } getLines() toString

val json: JsValue = Json.parse(model)

但我有个错误

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'non': was expecting 'null', 'true', 'false' or NaN
 at [Source: java.io.StringReader@a7e666; line: 1, column: 4]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1524)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:557)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2042)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2018)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._matchToken(ReaderBasedJsonParser.java:1870)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:657)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3031)
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2951)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1587)
    at play.api.libs.json.JacksonJson$.parseJsValue(JsValue.scala:486)
    at play.api.libs.json.Json$.parse(Json.scala:19)

有什么想法可以修复这个错误吗?

共有1个答案

戚俊美
2023-03-14

对我有效。来自REPL:

Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import play.api.libs.json._
import play.api.libs.json._

scala> val json = """
     | {
     |   "name": "a",
     |   "site": "b",
     |   "phones": ["c"],
     |   "duration": "d",
     |   "price-per-month": 10.00,
     |   "address": "e",
     |   "benefits": ["f"],
     |   "rating": 1
     | }
     | """
json: String =
"
{
  "name": "a",
  "site": "b",
  "phones": ["c"],
  "duration": "d",
  "price-per-month": 10.00,
  "address": "e",
  "benefits": ["f"],
  "rating": 1
}
"

scala> Json.parse(json)
res0: play.api.libs.json.JsValue = {"name":"a","site":"b","phones":["c"],"duration":"d","price-per-month":10,"address":"e","benefits":["f"],"rating":1}

所以问题可能出在你的方法上:

val model = fromInputStream { getClass getResourceAsStream "/model.json" } getLines() toString
 类似资料: