Scala 在JSON键中保留空白

Scala 在JSON键中保留空白,scala,gson,Scala,Gson,scala键中的空白不保留 case class c1( state :String, `Card No`:String ) 请求有效载荷 { "state": "HH", "Card No": "c1234", "xxxx": "xxxx" } 从gson转换为Json后,在代码中使用该case类时 它变成 {"state":"HH","Card$u0020No":"c1234","xxx":"xxx"} 我还没有尝试过使用gson进行JSON处理。我使用Play J

scala键中的空白不保留

case class c1(
  state :String,
  `Card No`:String
)
请求有效载荷

{
  "state": "HH",
  "Card No": "c1234",
  "xxxx": "xxxx"
}
从gson转换为Json后,在代码中使用该case类时 它变成

{"state":"HH","Card$u0020No":"c1234","xxx":"xxx"}
我还没有尝试过使用gson进行JSON处理。我使用Play JSON Play.api.libs.JSON框架

以下内容适用于JSON:

import play.api.libs.json._

case class MyClass(State: String, `Card No`: String)

// Play can convert any case class into a JsValue, using macros.
// you will need an implicit `Writes` for `MyClass`
implicit private val MyClassWrites = Json.writes[MyClass]

val myobj = MyClass(State = "Bangalore", `Card No` = "Bl@#1234")

val json = Json.toJson(myobj)

println(json)
给出以下JSON:

{"State":"Bangalore","Card No":"Bl@#1234"}
我签出gson lib后将更新此

更新:使用Gson

我希望您不要使用反引号来定义case类值,而是使用标准格式—可能是驼峰式的case。这将有助于保持代码更干净。 如下图所示:

case class MyClassA (state: String, cardNo: String)
并使用gson FieldNamingPolicy在构建JSON时获得正确的格式。 您可以创建一个gson构建器并执行如下操作

val status = MyClassA(State = "Bangalore", cardNo = "Bl@#1234")

val gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES)
    .setPrettyPrinting().create()

println(gson.toJson(status))
FieldNamingPolicy.UPPER_CAMEL_CASE_和_空格将确保在序列化为JSON格式时字段名的第一个字母大写,并且单词之间用空格分隔

上面将生成下面的JSON

{
  "State": "Bangalore",
  "Card No": "Bl@#1234"
}
此外,您还可以对任何字段名使用@SerializedName注释来指定序列化名称

使用注释 使用SerializedName,如下所示:

import com.google.gson.annotations.SerializedName
import com.google.gson.GsonBuilder
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder._

case class MyClassA (
    State: String, 
    @(SerializedName @scala.annotation.meta.field)("card no")
    cardNo: String
    )

object Test {
  def main(args: Array[String]): Unit = {
    val status = MyClassA(State = "Bangalore", CARD = "Bl@#1234")
    val gson = new GsonBuilder()
      .setPrettyPrinting().create()

    println(gson.toJson(status))
  }
}

UPD:下面是我正在使用的库依赖项:

libraryDependencies += "com.google.code.gson" % "gson" % "2.8.6"
您需要使用scala.annotation.meta中的元注释。参考此

我还没有尝试使用gson进行JSON处理。我使用Play JSON Play.api.libs.JSON框架

以下内容适用于JSON:

import play.api.libs.json._

case class MyClass(State: String, `Card No`: String)

// Play can convert any case class into a JsValue, using macros.
// you will need an implicit `Writes` for `MyClass`
implicit private val MyClassWrites = Json.writes[MyClass]

val myobj = MyClass(State = "Bangalore", `Card No` = "Bl@#1234")

val json = Json.toJson(myobj)

println(json)
给出以下JSON:

{"State":"Bangalore","Card No":"Bl@#1234"}
我签出gson lib后将更新此

更新:使用Gson

我希望您不要使用反引号来定义case类值,而是使用标准格式—可能是驼峰式的case。这将有助于保持代码更干净。 如下图所示:

case class MyClassA (state: String, cardNo: String)
并使用gson FieldNamingPolicy在构建JSON时获得正确的格式。 您可以创建一个gson构建器并执行如下操作

val status = MyClassA(State = "Bangalore", cardNo = "Bl@#1234")

val gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES)
    .setPrettyPrinting().create()

println(gson.toJson(status))
FieldNamingPolicy.UPPER_CAMEL_CASE_和_空格将确保在序列化为JSON格式时字段名的第一个字母大写,并且单词之间用空格分隔

上面将生成下面的JSON

{
  "State": "Bangalore",
  "Card No": "Bl@#1234"
}
此外,您还可以对任何字段名使用@SerializedName注释来指定序列化名称

使用注释 使用SerializedName,如下所示:

import com.google.gson.annotations.SerializedName
import com.google.gson.GsonBuilder
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder._

case class MyClassA (
    State: String, 
    @(SerializedName @scala.annotation.meta.field)("card no")
    cardNo: String
    )

object Test {
  def main(args: Array[String]): Unit = {
    val status = MyClassA(State = "Bangalore", CARD = "Bl@#1234")
    val gson = new GsonBuilder()
      .setPrettyPrinting().create()

    println(gson.toJson(status))
  }
}

UPD:下面是我正在使用的库依赖项:

libraryDependencies += "com.google.code.gson" % "gson" % "2.8.6"

您需要使用scala.annotation.meta中的元注释。请参阅此

谢谢。当我们使用FieldNamingPolicy-IDENTITY时,它应该使用与我们给定的名称相同的名称,对吗?因为我们有卡号、卡号数据和卡号数据这样的密钥。我需要在Scala中用@SerializedName作为case类。所以case类case类C1状态:String,@SerializedName卡号“卡号”:String@Ria使用注释更新了答案。如果它解决了问题,请接受答案:它在scala中不起作用。无法实例化,因为它不符合其自身类型[error]@SerializedName@scala.annotation.meta.fieldcard Nothing是否导入正确的包@Ria我正在添加上面的完整片段,并附带库依赖项谢谢。当我们使用FieldNamingPolicy-IDENTITY时,它应该使用与我们给定的名称相同的名称,对吗?因为我们有卡号、卡号数据和卡号数据这样的密钥。我需要在Scala中用@SerializedName作为case类。所以case类case类C1状态:String,@SerializedName卡号“卡号”:String@Ria使用注释更新了答案。如果它解决了问题,请接受答案:它在scala中不起作用。无法实例化,因为它不符合其自身类型[error]@SerializedName@scala.annotation.meta.fieldcard Nothing是否导入正确的包@Ria我正在添加上面带有库依赖项的完整代码段