I have a JSON string that contains the text bellow:
[ {"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"}
]
I want to retrieve the value of a row where id equals a specific value, for example if I'm looking for the id="offent", the returned value should be "oui".
I'm new to scala and I'm looking for a library that could make that easy without using native java code.
CodePudding user response:
You can use trustworthy org.json,
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>latest</version>
</dependency>
And than in the code:
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)
CodePudding user response:
For handy working with JSON in Scala you can use dijon library.
- Add dependency to your
build.sbt
:
libraryDependency = "me.vican.jorge" %% "dijon" % "0.6.0" // Use %%% instead of %% for Scala.js
- Turn on support of dynamic types by adding import clause:
import scala.language.dynamics._
or by setting the scala compiler option:
scalacOptions = "-language:dynamics"
- Add import of the package object of
dijon
for the main functionality:
import dijon._
- Use following code to parse, find and print the value:
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 })
- (Optionally) Import a package object of
jsoniter-scala-core
for extended functionality like scanning through JSON arrays fromjava.io.InputStream
and handle parsed values without holding all of them in the memory:
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
}