在scala中格式化JSON字符串 [英] Format JSON string in scala

查看:383
本文介绍了在scala中格式化JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个像这样的JSON字符串:
$ b有一个简单的方法来格式化一个JSON字符串在斯卡拉? $ b

  val json = {foo:{bar:{baz:T}}} 

可以使用一个函数< pre $ $ b $ f $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $



$ b我知道我在回答中所做的格式并不完美,明白这点。是的,是否可以不使用Play框架?

您可以使用 Json.prettyPrint 方法来格式 JsValue

  import play.api.libs.json.Json 

val str ={foo:{bar :{baz:T}}}

val jsValue = Json parse str
// JsValue = {foo:{bar:{ baz:T}}}

Json prettyPrint jsValue
// String =
// {
//foo:{
//bar:{
//baz:T
//}
//}
//}
scala.util.parsing.json
,你必须创建这样的方法由你自己。例如:

$ $ p $ def格式(t:Any,i:Int = 0):String = t match {
case o:JSONObject =>
o.obj.map {case(k,v)=>
*(i + 1)+ JSONFormat.defaultFormatter(k)+:+ format(v,i + 1)
} .mkString({\ n,,\\ \\ n,\\\
+* i +})

case a:JSONArray =>
a.list.map {
e =>格式(e,i + 1)
} .mkString([\ n,,\ n,\\\
+* i + ])

案例_ => JSONFormat defaultFormatter t

$ b $ val jsn = JSON.parseRaw({foo:{bar:{baz:T},arr: [1,2,x]},foo2:a})。get
// JSONType = {foo:{bar:{baz:T },arr:[1.0,2.0,x]},foo2:a}

格式(jsn)
// String =
// {
//foo:{
//bar:{
//baz:T
//},
//arr:[
// 1.0,
// 2.0,
//x
//]
//},
//foo2:a
//}

请注意,这是不是一个有效的实现。


Is there a straight forward way to format a JSON string in scala?

I have a JSON String like this:

val json = {"foo": {"bar": {"baz": T}}}

Can I use a function f such that:

f(json) = {
           "foo": 
                {"bar": 
                       {"baz": T}
                }
           }

I know the formatting I have done in my answer is no perfect, but you get the point. And yes, can it be done without using Play Framework?

解决方案

In case you are using Play Framework you could use Json.prettyPrint method to format JsValue:

import play.api.libs.json.Json

val str = """{"foo": {"bar": {"baz": "T"}}}"""

val jsValue = Json parse str
// JsValue = {"foo":{"bar":{"baz":"T"}}}

Json prettyPrint jsValue
// String = 
// {
//   "foo" : {
//     "bar" : {
//       "baz" : "T"
//     }
//   }
// }

In case you are using scala.util.parsing.json you have to create such method by yourself. For instance:

def format(t: Any, i: Int = 0): String = t match {
  case o: JSONObject =>
    o.obj.map{ case (k, v) =>
      "  "*(i+1) + JSONFormat.defaultFormatter(k) + ": " + format(v, i+1)
    }.mkString("{\n", ",\n", "\n" + "  "*i + "}")

  case a: JSONArray =>
    a.list.map{
      e => "  "*(i+1) + format(e, i+1)
    }.mkString("[\n", ",\n", "\n" + "  "*i + "]")

  case _ => JSONFormat defaultFormatter t
}

val jsn = JSON.parseRaw("""{"foo": {"bar": {"baz": "T"}, "arr": [1, 2, "x"]}, "foo2": "a"}""").get
// JSONType = {"foo" : {"bar" : {"baz" : "T"}, "arr" : [1.0, 2.0, "x"]}, "foo2" : "a"}

format(jsn)
// String = 
// {
//   "foo": {
//     "bar": {
//       "baz": "T"
//     },
//     "arr": [
//       1.0,
//       2.0,
//       "x"
//     ]
//   },
//   "foo2": "a"
// }

Note that this is not an efficient implementation.

这篇关于在scala中格式化JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆