scala对象转json字符串

1.样例类转换成为JSON字符串

  1. pom.xml文件
    <dependency>
        <groupId>org.json4s</groupId>
        <artifactId>json4s-native_2.11</artifactId>
        <version>3.5.4</version>
    </dependency>
    
  2. 代码操作
    import org.json4s.native.Serialization
    implicit val formats = org.json4s.DefaultFormats
    val orderInfoJson: String = Serialization.write(scala对象)
    

2.scala迭代器类型转成JSON字符串

  1. pom.xml文件
    <dependency>
    	<groupId>org.json4s</groupId>
    	<artifactId>json4s-native_2.11</artifactId>
    	<version>3.2.11</version>
    </dependency>
    
  2. 代码操作
    import org.json4s.native.JsonMethods
    import org.json4s.JsonDSL._
    JsonMethods.compact(JsonMethods.render(迭代器对象))
    

你可能感兴趣的:(scala,scala,json)