scala 使用 json4s 将 [String,Any] 映射到压缩 json 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27842906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map[String,Any] to compact json string using json4s
提问by jarandaf
I am currently extracting some metrics from different data sources and storing them in a map of type Map[String,Any]where the key corresponds to the metric name and the value corresponds to the metric value. I need this to be more or less generic, which means that values types can be primitive types or lists of primitive types.
我目前正在从不同的数据源中提取一些指标并将它们存储在一个类型的映射中,Map[String,Any]其中键对应于指标名称,值对应于指标值。我需要它或多或少是通用的,这意味着值类型可以是原始类型或原始类型列表。
I would like to serialize this map to a JSON-formatted string and for that I am using json4slibrary. The thing is that it does not seem possible and I don't see a possible solution for that. I would expect something like the following to work out of the box :)
我想将此映射序列化为 JSON 格式的字符串,为此我正在使用json4s库。问题是这似乎不可能,我看不到可能的解决方案。我希望像下面这样的东西开箱即用:)
val myMap: Map[String,Any] = ... // extract metrics
val json = myMap.reduceLeft(_ ~ _) // create JSON of metrics
Navigating through source codeI've seen json4sprovides implicit conversions in order to transform primitive types to JValue's and also to convert Traversable[A]/Map[String,A]/Option[A]to JValue's (under the restriction of being available an implicit conversion from Ato JValue, which I understand it actually means Ais a primitive type). The ~operator offers a nice way of constructing JObject's out of JField's, which is just a type alias for (String, JValue).
浏览我见过的源代码json4s提供了隐式转换,以便将基本类型转换为JValue's 并转换Traversable[A]/Map[String,A]/Option[A]为JValue's(在可用的限制下,从Ato的隐式转换JValue,我理解它实际上意味着A是一个原始类型)。该~运算符提供了一种从JObject's中构造's的好方法JField,它只是 的类型别名(String, JValue)。
In this case, map values type is Any, so implicit conversions don't take place and hence the compiler throws the following error:
在这种情况下,映射值类型为Any,因此不会发生隐式转换,因此编译器会抛出以下错误:
value ~ is not a member of (String, Any)
[error] val json = r.reduceLeft(_ ~ _)
Is there a solution for what I want to accomplish?
我想要完成的事情有解决方案吗?
回答by edi
Since you are actually only looking for the JSON string representation of myMap, you can use the Serializationobject directly. Here is a small example (if using the native version of json4s change the import to org.json4s.native.Serialization):
由于您实际上只是在寻找 的 JSON 字符串表示形式myMap,因此您可以Serialization直接使用该对象。这是一个小例子(如果使用原生版本的 json4s 将导入更改为org.json4s.native.Serialization):
EDIT: added formatsimplicit
编辑:添加formats隐式
import org.json4s.Hymanson.Serialization
implicit val formats = org.json4s.DefaultFormats
val m: Map[String, Any] = Map(
"name "-> "joe",
"children" -> List(
Map("name" -> "Mary", "age" -> 5),
Map("name" -> "Mazy", "age" -> 3)
)
)
// prints {"name ":"joe","children":[{"name":"Mary","age":5},{"name":"Mazy","age":3}]}
println(Serialization.write(m))
回答by Roman Kazanovskyi
json4s has method for it.
json4s 有它的方法。
pretty(render(yourMap))

