scala,从 JSON 数组中读取 JSON 字符串和 return 特定值

scala, read JSON string and return a specific value from the JSON array

我有一个 JSON 字符串,其中包含以下文本:

[   {"origin": "24u","name": "","id": "itcobtpbtc","value": "n"},
    {"origin": "24u","name": "","id": "offent","value": "oui"},
    {"origin": "24u","name": "","id": "network","value": "btip"},
    {"origin": "24u","name": "","id": "modpbx","value": "vide"},
    {"origin": "24u","name": "","id": "egtplg","value": "1"},
    {"origin": "24u","name": "","id": "zipbxcent","value": "non"},
    {"origin": "24u","name": "","id": "typpbx","value": "pabx"},
    {"origin": "24u","name": "","id": "egttyp","value": "gtr"},
    {"origin": "24u","name": "","id": "egtval","value": "240"},
    {"origin": "24u","name": "","id": "egtunt","value": "mn"},
    {"origin": "24u","name": "","id": "codec","value": "g71120ms"},
    {"origin": "24u","name": "","id": "modifseqsda","value": "non"},
    {"origin": "24u","name": "","id": "infra_mode_ha_site","value": "non"},
    {"origin": "24u","name": "","id": "numndi","value": "0238300385"},
    {"origin": "24u","name": "","id": "nbcanout","value": "0"},
    {"origin": "24u","name": "","id": "dbdrtc","value": "non"},
    {"origin": "24u","name": "","id": "egtcod","value": "s2"},
    {"origin": "24u","name": "","id": "numprtclp","value": "0044lfv4"},
    {"origin": "24u","name": "","id": "egtlib","value": "gtr 4h s2 lundi au samedi 8h a 18h "},
    {"origin": "24u","name": "","id": "dpmcible","value": "salto"},
    {"origin": "24u","name": "","id": "nbcanin","value": "0"},
    {"origin": "24u","name": "","id": "nbcan","value": "4"},
    {"origin": "24u","name": "","id": "nomsit","value": "foyer hebergement"},
    {"origin": "24u","name": "","id": "integrateur","value": "orange"}
]

我想检索 id 等于特定值的行的值,例如,如果我要查找 id="offent",则返回值应该是 "oui"。

我是 scala 的新手,我正在寻找一个无需使用本机 java 代码即可轻松实现的库。

为了在 Scala 中方便地使用 JSON,您可以使用 dijon 库。

  1. 将依赖项添加到您的 build.sbt:
libraryDependency += "me.vican.jorge" %% "dijon" % "0.6.0" // Use %%% instead of %% for Scala.js
  1. 通过添加导入子句打开对动态类型的支持:
import scala.language.dynamics._

或通过设置 scala 编译器选项:

scalacOptions += "-language:dynamics"
  1. 为主要功能添加 dijon 包对象的导入:
import dijon._
  1. 使用以下代码解析、查找并打印值:
val str =
  """[
    |    {"origin": "24u","name": "","id": "itcobtpbtc","value": "n"},
    |    {"origin": "24u","name": "","id": "offent","value": "oui"},
    |    {"origin": "24u","name": "","id": "network","value": "btip"},
    |    {"origin": "24u","name": "","id": "modpbx","value": "vide"},
    |    {"origin": "24u","name": "","id": "egtplg","value": "1"},
    |    {"origin": "24u","name": "","id": "zipbxcent","value": "non"},
    |    {"origin": "24u","name": "","id": "typpbx","value": "pabx"},
    |    {"origin": "24u","name": "","id": "egttyp","value": "gtr"},
    |    {"origin": "24u","name": "","id": "egtval","value": "240"},
    |    {"origin": "24u","name": "","id": "egtunt","value": "mn"},
    |    {"origin": "24u","name": "","id": "codec","value": "g71120ms"},
    |    {"origin": "24u","name": "","id": "modifseqsda","value": "non"},
    |    {"origin": "24u","name": "","id": "infra_mode_ha_site","value": "non"},
    |    {"origin": "24u","name": "","id": "numndi","value": "0238300385"},
    |    {"origin": "24u","name": "","id": "nbcanout","value": "0"},
    |    {"origin": "24u","name": "","id": "dbdrtc","value": "non"},
    |    {"origin": "24u","name": "","id": "egtcod","value": "s2"},
    |    {"origin": "24u","name": "","id": "numprtclp","value": "0044lfv4"},
    |    {"origin": "24u","name": "","id": "egtlib","value": "gtr 4h s2 lundi au samedi 8h a 18h "},
    |    {"origin": "24u","name": "","id": "dpmcible","value": "salto"},
    |    {"origin": "24u","name": "","id": "nbcanin","value": "0"},
    |    {"origin": "24u","name": "","id": "nbcan","value": "4"},
    |    {"origin": "24u","name": "","id": "nomsit","value": "foyer hebergement"},
    |    {"origin": "24u","name": "","id": "integrateur","value": "orange"}
    |]""".stripMargin
val json = parse(str)
println(json.toSeq.collect { case x if x.id.asString.contains("offent") => x.value })
  1. (可选)导入 jsoniter-scala-core 的包对象以实现扩展功能,例如扫描 java.io.InputStream 中的 JSON 数组,并处理已解析的值而不将它们全部保存在内存中:
import com.github.plokhotnyuk.jsoniter_scala.core._

val in: java.io.InputStream = new java.io.ByteArrayInputStream(str.getBytes)
scanJsonArrayFromStream[SomeJson](in) { x => 
  if (x.id.asString.contains("offent")) {
    println(x.value)
  }
  true
}

你可以使用值得信赖的org.json,

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>latest</version>
    </dependency>

与代码相比:

val json =
  """
    |[   {"origin": "24u","name": "","id": "itcobtpbtc","value": "n"},
    |    {"origin": "24u","name": "","id": "offent","value": "oui"},
    |    {"origin": "24u","name": "","id": "network","value": "btip"},
    |    {"origin": "24u","name": "","id": "modpbx","value": "vide"},
    |    {"origin": "24u","name": "","id": "egtplg","value": "1"},
    |    {"origin": "24u","name": "","id": "zipbxcent","value": "non"},
    |    {"origin": "24u","name": "","id": "typpbx","value": "pabx"},
    |    {"origin": "24u","name": "","id": "egttyp","value": "gtr"},
    |    {"origin": "24u","name": "","id": "egtval","value": "240"},
    |    {"origin": "24u","name": "","id": "egtunt","value": "mn"},
    |    {"origin": "24u","name": "","id": "codec","value": "g71120ms"},
    |    {"origin": "24u","name": "","id": "modifseqsda","value": "non"},
    |    {"origin": "24u","name": "","id": "infra_mode_ha_site","value": "non"},
    |    {"origin": "24u","name": "","id": "numndi","value": "0238300385"},
    |    {"origin": "24u","name": "","id": "nbcanout","value": "0"},
    |    {"origin": "24u","name": "","id": "dbdrtc","value": "non"},
    |    {"origin": "24u","name": "","id": "egtcod","value": "s2"},
    |    {"origin": "24u","name": "","id": "numprtclp","value": "0044lfv4"},
    |    {"origin": "24u","name": "","id": "egtlib","value": "gtr 4h s2 lundi au samedi 8h a 18h "},
    |    {"origin": "24u","name": "","id": "dpmcible","value": "salto"},
    |    {"origin": "24u","name": "","id": "nbcanin","value": "0"},
    |    {"origin": "24u","name": "","id": "nbcan","value": "4"},
    |    {"origin": "24u","name": "","id": "nomsit","value": "foyer hebergement"},
    |    {"origin": "24u","name": "","id": "integrateur","value": "orange"}
    |]""".stripMargin

case class YourModel(origin:String, name:String,id:String, value:String)

import collection.JavaConversions._
val jsonArray: JSONArray = new JSONArray(json)
val yourMap = jsonArray.toSeq.map(x=>{
  val jsonObject = new JSONObject(x.toString)
  YourModel(
    jsonObject.get("origin").toString,
    jsonObject.get("name").toString,
    jsonObject.get("id").toString,
    jsonObject.get("value").toString)
}).map(x=>x.id -> x).toMap

println(yourMap.get("offent").get.value)