简体   繁体   English

Java / Scala lib将点符号解析为Json字符串

[英]Java/Scala lib to parse dot-notation to Json string

I wanna parse some dot-notation map to a json string, for example I have: 我想将一些点符号映射解析为json字符串,例如,我有:

input["top.second.third0"] = 0
input["top.second.third1"] = "hello"

And then I want to get the following json string from this map : 然后我想从此地图获取以下json字符串:

{"top":{
     "second":{
         "third0":0,
         "third1":"hello"
        }
   }
}

I know how to generate it by using split, I am asking if there is a Java/Scala library that can do that for me? 我知道如何使用split生成它,我在问是否有Java / Scala库可以为我做到这一点?

For Play JSON, you can parse such expression to JsPath and then use it. 对于Play JSON,您可以将此类表达式解析为JsPath ,然后使用它。

import play.api.libs.json._

val components = input.split("\\.")

components.headOption match {
  case Some(p) => components.tail.foldLeft(JsPath \ p) { _ \ _ }
  case _ => /* cannot parse */ ???
}

I do something like this to finish this task. 我这样做是为了完成这项任务。

  val input = "a.b.c"
  val components = input.split("\\.")

  val jsPath = components.headOption match {
    case Some(p) => components.tail.foldLeft(JsPath \ p) { _ \ _ }
    case _ => /* cannot parse */ ???
  }
  val obj = JsPath.createObj(jsPath -> JsString("Hello"))
  Logger.debug("after write" + obj.toString)

I don't know why but seems everybody is using play if you use scala. 我不知道为什么,但是如果您使用Scala,似乎每个人都在使用游戏。

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

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