scala使用jackson读取json时出错~

hkkiven 发布于 2016/05/31 11:09
阅读 1K+
收藏 0
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}"




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]


我明明已经定义了所有字段了,为啥还报这个错误~~

加载中
0
一万
一万
类的字段没有set和get方法
返回顶部
顶部