Parse JSON Array in Scala

前提是你 提交于 2020-01-23 18:00:17

问题


I have this jsArray (json Array) and I am using import play.api.libs.json._ library.

[{”device”:”Samsung S8”,”android”:true},
{”device”:”iPhone 8”,”android”:false},
{”device”:”MacBook Air Pro”,”android”:false},
{”device”:”Dell XPS”,”android”:false}]

I want to traverse through this json array in Scala. This array is assigned to var dependency. I want to get the device names which are android. How do I do that?


回答1:


You can try something like this:

val jsonString: String = "[{\"device\":\"Samsung S8\",\"android\":true {\"device\":\"iPhone8\",\"android\":false}, {\"device\":\"MacBook Air Pro\",\"android\":false},{\"device\":\"Dell XPS\",\"android\":false}]"
val jsonList: List[JsValue] = Json.parse(jsonString).as[List[JsValue]]
val filteredList: List[JsValue] = jsonList.filter(json => (json \ "android").as[Boolean])


来源:https://stackoverflow.com/questions/51680206/parse-json-array-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!