简体   繁体   中英

Scala and JavaScript's JSON.stringify

When I stringify an object in Scala, the resulting string doesn't match with what JavaScript is expecting the string to be when it attempts to parse the string.

For example, a Scala object may look like the following when the value of Scala's JSON.stringify() is added to the "new Cookie()" instance and the cookie is read by JavaScript.

"{\"id\":\"ABCD\"}"

However, JavaScript's JSON.parse() method is expecting

{"id":"ABCD"}

The issue is that I am attempting to save info in a cookie that both Scala and JavaScript can access and modify. I would like to place the info in the cookie using a common format. Is there a way to stringify the JSON in Scala so it can be parsed by JavaScript and visa-versa? Should I use a different format?

================================

UPDATE:

Here is how I am stringifying the JSON and placing the string in a cookie all within Scala:

  var sTokenID:String = "ABCD"
  val nDaysExpire:Int = 2000
  val nSecondsExpire:Int = nDaysExpire * 24 * 60 * 60
  val jsonObject = Json.toJson(
    Map(
      "id" -> Json.toJson(sTokenID)
    )
  )
  val sValue:String = Json.stringify(jsonObject)
  val cookie:Cookie = new Cookie(_sCookieID, sValue, Option(nSecondsExpire), "/", scala.None, false, false)

jsonObject.toString()应该可以解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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