在 Scala 中将 JSON 字符串转换为 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30884841/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:56:26  来源:igfitidea点击:

Converting JSON string to a JSON object in Scala

jsonscala

提问by drunkenfist

I want to convert a simple JSON string such as {"Name":"abc", "age":10}to the corresponding JSON object (not a custom Scala object such as "Person"). Does Scala support any in-built methods to convert a String to a JSON object?

我想将一个简单的 JSON 字符串转换为{"Name":"abc", "age":10}相应的 JSON 对象(而不是自定义的 Scala 对象,例如“Person”)。Scala 是否支持任何内置方法将字符串转换为 JSON 对象?

I'm not going to have any complex JSON operations. I just need to convert the String to a JSON object. What is the simplest way to do this? I'm new to Scala, so I apologize if this question sounds very basic.

我不会有任何复杂的 JSON 操作。我只需要将字符串转换为 JSON 对象。什么是最简单的方法来做到这一点?我是 Scala 的新手,所以如果这个问题听起来很基本,我深表歉意。

Thanks.

谢谢。

回答by DemetriKots

Note:Technically, there is no longer a core Scala "native" way of parsing JSON. You should use an external, supported library like Spray JSON or Play JSON.

注意:从技术上讲,不再有解析 JSON 的核心 Scala“本机”方式。您应该使用外部受支持的库,例如 Spray JSON 或 Play JSON。

As of Scala 2.11 the parser-combinator library is no longer included in the core language jar and needs to be added separately to your project. Further, the JSON parser has since been deprecated in the community supported version of the parser-combinator library. I would not recommend using this library.

从 Scala 2.11 开始, parser-combinator 库不再包含在核心语言 jar 中,需要单独添加到您的项目中。此外,JSON 解析器在社区支持的 parser-combinator 库版本中已被弃用。我不建议使用这个库

You can still add it to your project, if you choose to, by adding the following to your build.sbt:

如果您愿意,您仍然可以将其添加到您的项目中,方法是将以下内容添加到您的 build.sbt 中:

libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4"

libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4"

You can find the source code for the library at https://github.com/scala/scala-parser-combinators.

您可以在https://github.com/scala/scala-parser-combinators找到该库的源代码。



Since you asked specifically about Scala's native facilities for JSON parsing – the package you are looking for is the scala.utils.parsing.json. Something like the following should work:

由于您专门询问了 Scala 用于 JSON 解析的本机工具 - 您正在寻找的包是 scala.utils.parsing.json。像下面这样的东西应该工作:

import scala.util.parsing.json._

val parsed = JSON.parseFull("""{"Name":"abc", "age":10}""")

parsedwill take on the value: Some(Map(Name -> abc, age -> 10.0))

parsed将采用以下值: Some(Map(Name -> abc, age -> 10.0))

回答by Denis Rosca

You might want to use a library like Spray JSON. It provides a lot of easy to use functionality for converting to and from JSON. If you decide to use Spray JSON you can do this:

您可能想要使用像Spray JSON这样的库。它提供了许多易于使用的功能,用于在 JSON 之间进行转换。如果您决定使用 Spray JSON,您可以这样做:

import spray.json._
// some code here
val json = "your json string here".parseJson

回答by Oleg

Also you can use Json Library from play framework, but can be used as standalone lib also. This library based on good but abandoned Jerksonproject, which is a Scala wrapper around the super-fast Java based JSON library, Hymanson. And it has very rich and good documented toolset for working with JSON - transofrmers, validators and etc.

您也可以使用play framework 中的Json 库,但也可以用作独立库。该库基于优秀但已被废弃的Jerkson项目,该项目是围绕基于 Java 的超快速 JSON 库 Hymanson 的 Scala 包装器。它有非常丰富和良好的文档工具集,用于处理 JSON - 转换器、验证器等。

import play.api.libs.json._

val json: JsValue = Json.parse("""{"a":1}""")

To use this lib without play just install it in build.sbt with string

要在不播放的情况下使用此库,只需将其安装在带有字符串的 build.sbt 中

libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.0"

回答by Guda uma shanker

The parseFullreturns in-terms of Some(Map), parseRawreturns in terms of Some(JSONObject)

parseFull回报Some(Map),parseRaw回报Some(JSONObject)

import scala.util.parsing.json._

val parsed = JSON.parseRaw("""{"Name":"abc", "age":10}""").getOrElse(yourDefault)

parsedis the JSONObject

parsed是 JSONObject