开发者社区> 问答> 正文

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}"




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]


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

展开
收起
爱吃鱼的程序员 2020-06-09 14:17:05 761 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    类的字段没有set和get方法

    2020-06-09 14:17:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Just Enough Scala for Spark 立即下载
JDK8新特性与生产-for“华东地区scala爱好者聚会” 立即下载
File Format Benchmark - Avro, JSON, ORC, & Parquet 立即下载