我必须提取 JSON 响应的信息并评估是否存在某些文件。我正在使用以下方法定义:
override def hasField(field: Field): Boolean = {
val schema = parse(httpClient.doGet(url + Solr5EndpointUris.schemaOverviewEndpoint)).extract[Map[String, Any]]
val fieldsNames: List[String] = schema.get("schema") match {
case schema: Some[Map[String, Any]] => schema.get(if (field.dynamic) "dynamicFields" else "fields") match {
case fields: List[Map[String, Any]] => fields.map {
case field: Map[String, Any] => field.get("name") match {
case name: Some[String] => name.getOrElse("")
}
}
case _ => throw new ApiException("Malformed Response! Missing definition for schema > fields/dynamicFields.")
}
case _ => throw new ApiException("Malformed Response! Could not extract schema from JSON.")
}
fieldsNames.contains(field.name)
}
该方法通过模式匹配检查 JSON 响应,如果存在具有特定名称的字段,则应返回 true。示例 JSON 响应可能如下所示:
{
"responseHeader":{
"status":0,
"QTime":2},
"schema":{
"name":"example-data-driven-schema",
"version":1.5,
"uniqueKey":"id",
"fieldTypes":[],
"fields":[{
"name":"id",
"type":"string",
"multiValued":false,
"indexed":true,
"required":true,
"stored":true}],
"dynamicFields":[],
"copyFields":[]
}
}
这个实现确实有效,但我很确定有一个更直接/不太复杂的实现来实现这一点。我也收到很多警告,如下所示:
SchemaManager.scala:38: 类型模式 Some[Map[String,Any]] 中的非变量类型参数 Map[String,Any] 未选中,因为它已被擦除消除
谁能提供更好的解决方案,和/或解释我收到的警告?