摆脱javax.json.Json生成的json中的反斜杠

时间:2015-07-18 19:02:41

标签: json scala

我使用javax.json和Google gson库在Scala程序中生成了一个Json。

然而,到目前为止我已成功完成的任务(问题JSON )存在问题:

  • 我不需要反斜杠。
  • equivalent_comps元素(数组)格式不正确。

问题JSON:

   {
    "outcomes": [
       {
         "some_line": "Hampton Court",
         "some_name": "Divya Katdare",
         "equivalent_comps": [
           "{\"comp_type\" : \"SNumber\", \"comp_name\" : \"636-74-1234\"} {\"comp_type\" : \"AENumber\", \"comp_name\" : \"843497127459638\"}{\"comp_type\" : \"VNumber\", \"comp_name\" : \"5111111111111111\"}"
          ]
        }
     ]
   }

我想要完成的事情:

{
"outcomes": [
  {
    "some_line": "Hampton Court",
    "some_name": "Divya Katdare",
    "equivalent_comps": [
       {
         "comp_type" : "SNumber", 
         "comp_name" : "636-74-1234"
      },
     {
        "comp_type" :  "AENumber", 
        "comp_name" : "843497127459638"
    },
    {
        "comp_type" : "VNumber", 
        "comp_name" : "5111111111111111"
      }
    ]
  }
]
 }

以下是我完成任务的步骤:

第0步:在StackOverflow和Google上查找类似的问题。

在我发表这篇文章之前,我查了资源并做了我的研究。然而,即使经过所有的研究,试验和错误,我也没有成功:

  1. String.replaceAll single backslashes with double backslashes
  2. String replace a Backslash
  3. Why String.ReplaceAll() in java requires 4 slashes "\\\\" in regex to actually replace "\"?
  4. http://www.xyzws.com/javafaq/how-many-backslashes/198
  5. 第1步:导入所需的库

    import javax.json.{JsonValue, Json}
    import com.google.gson.{GsonBuilder, JsonParser}
    

    第2步:创建StringWriter

    var writer = new StringWriter()
    

    第3步:创建生成器

    val generator = Json.createGenerator(writer)
    

    第4步:创建我想最终转换为我想要的JSON的初始列表

    val componentList = List((SNumber,636-74-1234), (AENumber,843497127459638), (VNumber,5111111111111111))
    

    第5步:遍历componentList并创建List JSONObjects

    val componentListAsJsonObjectList = 
      for(component <- componentList) 
        yield (scala.util.parsing.json.JSONObject(
          Map("comp_type" -> component._1,"comp_name" -> component._2)
        ))
    
    println("componentListAsJsonObjectList is: " + componentListAsJsonObjectList)
    

    以上println会产生以下输出:

    List({"comp_type" : "SNumber", "comp_name" : "636-74-1234"}, {"comp_type" : "AENumber", "comp_name" : "843497127459638"}, {"comp_type" : "VNumber", "comp_name" : "5111111111111111"})
    

    第6步:将List的{​​{1}}转换为字符串

    JSONObjects
    打印出

    val componentListAsJsonString = componentListAsJsonObjectList.mkString println("componentListAsJsonString is: " + componentListAsJsonString)

    componentListAsJsonString

    第7步:在生成器上调用方法,使用{"comp_type" : "SNumber", "comp_name" : "636-74-1234"}{"comp_type" : "AENumber", "comp_name" : "843497127459638"}{"comp_type" : "VNumber", "comp_name" : "5111111111111111"}

    创建所需的JSON
    componentListAsJsonString

    步骤8:关闭发电机

    generator
      .writeStartObject()
        .writeStartArray("outcomes")
          .writeStartObject()   
            .write("spme_line", "I am fine")
            .write("some_name", "Divya Katdare")   
            .writeStartArray("equivalent_comps")   
              .write(componentListAsJsonString)
            .writeEnd()
          .writeEnd()
        .writeEnd()
      .writeEnd()
    

    第9步:使用gson的generator.close()

    对JSON进行整理
    JsonParser

    步骤10:检查创建的JSON并将其与所需的JSON格式进行比较

    val prettyJson = makeJsonPretty(writer.toString.trim)
    
    def makeJsonPretty(generatedJson: String): String = {
      val parser = new JsonParser()
      val json = parser.parse(generatedJson).getAsJsonObject
      val prettyGson = new GsonBuilder().setPrettyPrinting().create()
      val prettyJson = prettyGson.toJson(json)
      prettyJson
    }
    

    这会在我的问题开头输出 Problem Json

    最后我创造了我一直想要的Json。然而,它有所有这些反斜杠。

    第11步:我验证了JSON

    它检查出有效的Json,即使它不是我想要的。

    感谢修复此JSON所需的任何建议或帮助。

1 个答案:

答案 0 :(得分:1)

你绑定javax.json? json有很多scala库没有这样的副作用。仅举例:

import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.write
implicit val format = DefaultFormats

case class Comp(comp_type: String, comp_name: String)
case class Outcome(some_line: String, some_name: String, equivalent_comps: List[Comp])
case class A(outcomes: List[Outcome])
val componentList = List(
  Comp("SNumber","636-74-1234"),
  Comp("AENumber","843497127459638"),
  Comp("VNumber","5111111111111111")
)
val oc = Outcome("Hampton Court", "Divya Katdare", componentList)
val a = A(List(oc))
pretty(parse(write(a)))

和结果:

res0: String = {
  "outcomes" : [ {
    "some_line" : "Hampton Court",
    "some_name" : "Divya Katdare",
    "equivalent_comps" : [ {
      "comp_type" : "SNumber",
      "comp_name" : "636-74-1234"
    }, {
      "comp_type" : "AENumber",
      "comp_name" : "843497127459638"
    }, {
      "comp_type" : "VNumber",
      "comp_name" : "5111111111111111"
    } ]
  } ]
}
相关问题