An Api request returns a Json string like below. I've been trying to parse in Scala, add the "name" field to a list and then filter this list using another list. The API is from a third party so the format cannot be modified.
Example list (2 entries in values here but there can be up to 300):
{
"size": 20,
"values": [{
"name": "mullock",
"upstatus": "Green",
"details": {
"key": "rupture farms",
"server": "mudos",
"owner": "magog_cartel",
"type": "NORMAL",
"links": {
"self": [{
"address": "https://mudos.com:port/access"
}]
}
}
},
{
"name": "tassadar",
"upstatus": "Orange",
"details": {
"key": "archon",
"server": "protoss",
"owner": "aspp67",
"type": "NORMAL",
"links": {
"self": [{
"address": "https://aiur.com:port/access"
}]
}
}
}
],
"limit": 100
}
I've attempted to unmarshal the string using jackson and some suggested functions (below) which are used in other parts of the application but I don't fully understand this.
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
object Json {
/* Json/Scala translation utilities
*/
val mapper = {
val _mapper = new ObjectMapper() with ScalaObjectMapper
_mapper.registerModule(DefaultScalaModule)
_mapper
}
def dump(obj: Any): String = {
mapper.writeValueAsString(obj)
}
def dumpObj(fields: (Any, Any)*): String = {
dump(fields.toMap)
}
def read[T: Manifest](content: String): T = {
mapper.readValue(content, mapper.constructType[T])
}
def readObj(content: String): Map[Any, Any] = {
return read[Map[Any, Any]](content)
}
}
- How can I access nested elements of in object Map(Any, Any)?
- Is this the correct way to deserialize a JSON String?
Any help greatly appreciated!
Anycan really be any, including string, int, float, boolean or custom object. You need to deseralize it to some particular class in order to access nested structures. All three libs I've outlined in my comment allow that.