如何根据特定条件从 HTTP 响应字符串创建列表?

How to create a List from a HTTP response string based on certain conditions?

我正在尝试在 Scala.

中解析 HTTPS 请求的结果

HTTPS 响应是一个 String,如下所示:

{
  "rows":
  [
    {
      "log_forwarding_ip":"",
      "device_name":"AD1",
      "id":"51",
      "mgmt_ip_addr":"192.168.25.150",
      "log_forwarding":"1",
      "isActive":"0"
    },
    {
      "log_forwarding_ip":"192.168.1.1",
      "device_name":"WIN-SRV2019",
      "id":"50",
      "mgmt_ip_addr":"192.168.25.151",
      "log_forwarding":"1",
      "isActive":"1"
    },
    {
      "log_forwarding_ip":"129.168.1.2",
      "device_name":"PA",
      "id":"3",
      "mgmt_ip_addr":"192.168.1.161",
      "log_forwarding":"1",
      "isActive":"1"
    }
  ],
  "status":1
}

我必须为所有 id 创建一个 List,其中 isActivelog_forwarding 都等于 1.

到目前为止我所做的是:

object syncTables {
  def main(args: Array[String]): Unit = {
    case class deviceInfo(log_forwarding_ip: String, device_name: String, id: String, 
                          mgmt_ip_addr: String, log_forwarding: String, isActive: String)
    try {
      val r = requests.get("https://192.168.1.253/api/device/deviceinfo.php", verifySslCerts = false)
      if (r.statusCode == 200) {
        val x = r.text
        println(x)
      } else {
        println("Error in API call: "+r.statusCode)
      }
    }
  }
}

现在我真的很困惑下一步该怎么做才能达到我的结果。
我对JSON完全陌生,所以我不知道哪个JSON我应该使用的库。
我尝试使用 Play Framework,但对我来说似乎很复杂。

Scala 是否提供类似于 Pythonjson 模块,使用 dictionarieslists 可以轻松完成此任务。

我正在使用 Scala 2.11.12com.lihaoyi.requests
非常感谢任何形式的帮助。
在此先致谢。

使用json4s解析JSON字符串。让我们调用你的 JSON 输入字符串 json,然后你可以这样做

import org.json4s._
import org.json4s.jackson.JsonMethods._

case class DeviceInfo(log_forwarding_ip: String, device_name: String, id: String,
                      mgmt_ip_addr: String, log_forwarding: String, isActive: String)

implicit val formats: Formats = DefaultFormats  // Brings in default date formats etc.

val parsedJson = parse(json)
val deviceInfos = parsedJson match {
  case JObject(head :: _) =>
    head._2
      .extract[List[DeviceInfo]]
      .filter(_.isActive == 1 && _.log_forwarding == 1)
}

这将输出

val res0: List[DeviceInfo] = List(DeviceInfo("192.168.1.1","WIN-SRV2019","50","192.168.25.151","1","1"),DeviceInfo("129.168.1.2","PA","3","192.168.1.161","1","1"))