简体   繁体   English

使用gson库将Scala Seq对象转换为JSON

[英]Scala Seq Object to JSON using gson Library

I am trying to do the following: 我正在尝试执行以下操作:

`import com.google.gson.Gson

 object someObject extends App{
      case class entries(a:Seq[String], b:String){
         def toJson() = new Gson().toJson(this)
      }

      val one : Seq[String] = List("m","n")
      val two : String = "ok"

      val ans = entries(a = one, b = two)
      println(ans.toJson)
 }`

I am getting the following output: 我得到以下输出:

 `{"a":{},"b":"ok"}`

Can i know why i am not able to convert the seq to json? 我能知道为什么我无法将seq转换为json吗? I tried other libraries including spray.json and json4s but no help. 我尝试了其他库,包括spray.json和json4s,但没有帮助。 Tried a lot over this. 尝试了很多。

Any help is appreciated. 任何帮助表示赞赏。 Other similar answers did not help. 其他类似的答案也无济于事。

Since Gson library is a Java library, I would try to use Java collections instead of Scala collections. 由于Gson库是Java库,因此我将尝试使用Java集合而不是Scala集合。 With java.util.List instead of scala.collections.Seq and java.util.ArrayList instead of scala List you should get the right output: 使用java.util.List代替scala.collections.Seqjava.util.ArrayList代替scala List,您应该获得正确的输出:

import com.google.gson.Gson
import scala.collection.JavaConversions._
import java.util.ArrayList
import java.util.List

object someObject extends App {

  case class entries(a: List[String], b: String) {
    def toJson() = new Gson().toJson(this)
  }

  val one: List[String] = new ArrayList[String]
  one.add("m")
  one.add("n")
  val two: String = "ok"

  val ans = entries(a = one, b = two)
  println(ans.toJson)
}

Output now is: 现在的输出是:

{"a":["m","n"],"b":"ok"} {“ a”:[“ m”,“ n”],“ b”:“确定”}

You can try 你可以试试

case class Entry(a:Array[String], b:String)
val e = Entry(Array("a", "b"), "c")
val gson = new Gson
println(gson.toJson(e))

gson can parse Array type the output will be: {"a":["a","b"],"b":"c"} gson可以解析Array类型,输出为:{“ a”:[“ a”,“ b”],“ b”:“ c”}

Try jsoniter-scala , it supports all Scala collections, also it is most efficient serializer to JSON for Scala. 尝试jsoniter-scala ,它支持所有 Scala集合,这也是Scala到JSON的最有效的序列化器。 Here are results of benchmarks which compare parsing & serialization performance of this library vs. Jackson, Circe and Play-JSON libraries using JDK 8. 以下是基准测试的结果,这些基准测试比较了该库与使用JDK 8的Jackson,Circe和Play-JSON库的解析和序列化性能。

Add the library to your dependencies list 将库添加到您的依赖项列表

libraryDependencies ++= Seq(
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "0.29.2" % Compile, 
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "0.29.2" % Provided // required only in compile-time
)

Generate codecs for your case classes, collections, etc. 为您的案例类,集合等生成编解码器。

import com.github.plokhotnyuk.jsoniter_scala.macros._
import com.github.plokhotnyuk.jsoniter_scala.core._

case class entries(a: Seq[String], b: String)

implicit val codec: JsonValueCodec[entries] = JsonCodecMaker.make[entries](CodecMakerConfig())

Now you can use it for serialization 现在您可以将其用于序列化

writeToArray(entries(a = Seq("m", "n"), b = "ok"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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