scala使用jackson读取json时出错~?报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

scala使用jackson读取json时出错~?报错

爱吃鱼的程序员 2020-06-09 14:17:05 27

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]


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

JSON Scala 数据格式
分享到
取消 提交回答
全部回答(1)
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程