Scala 使用json4s时如何设置Jackson解析器特性?
我在尝试用json4s解析JSON时收到以下错误:Scala 使用json4s时如何设置Jackson解析器特性?,scala,jackson,json4s,Scala,Jackson,Json4s,我在尝试用json4s解析JSON时收到以下错误: Non-standard token 'NaN': enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow 如何启用此功能?假设您的对象名为mapper: val mapper = new ObjectMapper() // Configure NaN here mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMB
Non-standard token 'NaN': enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow
如何启用此功能?假设您的对象名为mapper
:
val mapper = new ObjectMapper()
// Configure NaN here
mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true)
...
val json = ... //Get your json
val imported = mapper.readValue(json, classOf[Thing]) // Thing being whatever class you're importing to.
@纳撒尼尔·福特,谢谢你让我走上了正确的道路 最后我查看了parse()方法的源代码(这是我首先应该做的)。这项工作:
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.ObjectMapper
import org.json4s._
import org.json4s.jackson.Json4sScalaModule
val jsonString = """{"price": NaN}"""
val mapper = new ObjectMapper()
// Configure NaN here
mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true)
mapper.registerModule(new Json4sScalaModule)
val json = mapper.readValue(jsonString, classOf[JValue])
虽然上面的答案仍然正确,但需要修改的是,由于Jackson 2.10
JsonParser.Feature.ALLOW\u NON\u NUMERIC\u NUMBERS
已被弃用
配置正确的NaN
处理的可持续方法如下:
val mapper=JsonMapper.builder().enable(JsonReadFeature.ALLOW_NON_NUMERIC_number).build();
//现在,您的解析
如果答案对你有帮助,不要忘记投票/接受答案!你将来更有可能得到帮助。嗯。。。我认为当我阅读JSON文档时会出现错误:val JSON=parse(jsonString)@arosca如果没有更多的上下文/代码,恐怕我无法完全看到这种关系。