2016-08-24 101 views
1

我收到以下错误,同时尝试与json4s解析JSON:如何在使用json4s时设置Jackson解析器功能?

Non-standard token 'NaN': enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow 

如何启用此功能?

+0

如果他们帮助你,请不要忘记upvote/accept answers!你将来更有可能获得帮助。 –

回答

0

假设你ObjectMapper对象被命名为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. 
+0

嗯...我认为我的错误发生在我阅读JSON文档时: val json = parse(jsonString) – arosca

+0

@arosca恐怕没有更多的上下文/代码我不能完全看到关系。 –

0

@Nathaniel福特,设置我在正确的道路上感谢!

我最终看到了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])