在 Scala 中解析 JSON 数组

Aak*_*ary 1 arrays json scala playframework

我有这个 jsArray (json Array) 并且我正在使用import play.api.libs.json._库。

[{”device”:”Samsung S8”,”android”:true},
{”device”:”iPhone 8”,”android”:false},
{”device”:”MacBook Air Pro”,”android”:false},
{”device”:”Dell XPS”,”android”:false}]
Run Code Online (Sandbox Code Playgroud)

我想在 Scala 中遍历这个 json 数组。该数组被分配给var dependency。我想获取 android 的设备名称。我怎么做?

Jos*_*ero 6

你可以尝试这样的事情:

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])
Run Code Online (Sandbox Code Playgroud)

  • 需要对行 Json.parse(jsonString).as[List[JsValue]] 执行什么导入操作 (2认同)