简体   繁体   中英

Scala:No suitable constructor found for type [simple type, class Thing]: can not instantiate from JSON object

I have tried to test the following code to reading Json , but it false.

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)

Here is the Json I want to read: "{\\"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}"

I get the following response: enter image description here

I know 'type' is a keyword in Scala. So, I tried using @JsonProperty.It's that my code is right ? OR there are any other problems with class JsonLong?

I find one way to deal with this log. But not use jackson,use json4s to catch the key value.

import org.json4s._
import org.json4s.jackson.JsonMethods._

implicit val formats = DefaultFormats 

case class JsonLong(
  var fdn: String=null,
  var `type`: String=null, 
  var vid: String=null,
  var version: String=null,
  var device_id: String=null,
  var ip: String=null,
  var timestamp: Long=0L
)

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 jsontest = parse(jsoninput, useBigDecimalForDouble = true)
    jsontest.extract[JsonLong]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM