Scala使用jackson解析JSON

  • jackson依赖
    
        com.fasterxml.jackson.module
        jackson-module-scala_2.11
        2.6.5
    
    
  • 实例,@BeanProperty为样本类生成 Java 风格的 get、set方法,@JsonProperty注解映射字段
    import java.util
    import com.fasterxml.jackson.annotation.JsonProperty
    
    import scala.beans.BeanProperty
    import scala.collection.JavaConversions._
    
    object parse {
      case class Item(@JsonProperty("bw") @BeanProperty bw: String, @JsonProperty("cdn") @BeanProperty cdn: Long, @JsonProperty("ct") @BeanProperty ct: Long)
      case class Outer(@JsonProperty("video_bandwidths") @BeanProperty items: util.ArrayList[Item])
    
      def main(args: Array[String]): Unit = {
        import com.fasterxml.jackson.core.JsonParser.Feature
        import com.fasterxml.jackson.databind.ObjectMapper
    
        val mapper = new ObjectMapper
        //解析器支持解析单引号
        mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true)
        //解析器支持解析结束符
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true)
    
        val jsonMap = mapper.readValue("{\"video_bandwidths\":[ { \"bw\" : \"484456834\" , \"cdn\" : 0 , \"ct\" : 0} , { \"bw\" : \"160477600\" , \"cdn\" : 0 , \"ct\" : 1} , { \"bw\" : \"603954332\" , \"cdn\" : 0 , \"ct\" : 2}]}", classOf[Outer]) //转换为HashMap对象
    
        jsonMap.getItems.foreach(println)
        println(jsonMap.getItems.get(0).getBw)
        jsonMap.getItems.foreach(a=>println(a.getBw))
      }
    }
    

你可能感兴趣的:(Scala使用jackson解析JSON)