scala使用jackson读取json时出错~
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.spark.{SparkContext, SparkConf}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.sql.Date
import java.text.SimpleDateFormat
import org.slf4j.LoggerFactory
@JsonCreator
case class JsonLong{
@JsonProperty("fdn") var fdn: String = null
@JsonProperty("type") var tpy: String= null
@JsonProperty("vid") var vid: String = null
@JsonProperty("version") var version: String = null
@JsonProperty("device_id") var device_id: String = null
@JsonProperty("ip") var ip: String = null
@JsonProperty("timestamp") var timestamp: Long = 0L
}
def jsonString(logjson:String):JsonLong ={
val mapper = new ObjectMapper()
val record = mapper.readValue(logjson, classOf[JsonLong])
record
}
val jsoninput = "{"fdn":"FDNB2023750","type":"0","vid":"2246195","version":"1.0","device_id":"HM+NOTE+1TD_0c-1d-af-7e-1e-a3_865813020970745","ip":"106.118.164.215","timestamp":1463847764}"
import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.spark.{SparkContext, SparkConf}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.sql.Date
import java.text.SimpleDateFormat
import org.slf4j.LoggerFactory
@JsonCreator
case class JsonLong{
@JsonProperty("fdn") var fdn: String = null
@JsonProperty("type") var tpy: String= null
@JsonProperty("vid") var vid: String = null
@JsonProperty("version") var version: String = null
@JsonProperty("device_id") var device_id: String = null
@JsonProperty("ip") var ip: String = null
@JsonProperty("timestamp") var timestamp: Long = 0L
}
def jsonString(logjson:String):JsonLong ={
val mapper = new ObjectMapper()
val record = mapper.readValue(logjson, classOf[JsonLong])
record
}
val jsoninput = "{"fdn":"FDNB2023750","type":"0","vid":"2246195","version":"1.0","device_id":"HM+NOTE+1TD_0c-1d-af-7e-1e-a3_865813020970745","ip":"106.118.164.215","timestamp":1463847764}"
val jsonRDD = jsonString(jsoninput)
报错为com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class $iwC$$iwC$JsonLong]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: {"fdn":"FDNB2023750","type":"0","vid":"2246195","version":"1.0","device_id":"HM+NOTE+1TD_0c-1d-af-7e-1e-a3_865813020970745","ip":"106.118.164.215","timestamp":1463847764}; line: 1, column: 2]
我明明已经定义了所有字段了,为啥还报这个错误~~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类的字段没有set和get方法