使用 JSON 字符串中的嵌套映射

发布于 2024-12-09 19:02:44 字数 780 浏览 0 评论 0原文

给定一个像这样的 JSON 字符串:

{"Locations":
  {"list":
    [
      {"description": "some description", "name": "the name", "id": "dev123"},
      {"description": "other description", "name": "other name", "id": "dev59"}
    ]
  }
}

我想从解析上述字符串的函数返回“id”列表。 JSON.parseFull() (来自 scala.util.parsing.json)给我一个 Option[Any] 类型的结果。 Scala REPL 将其显示为 Some(Map(Locations -> Map(list -> List(Map(id -> dev123, ...)),作为 Scala 的初学者,我很困惑 ”

Scala API 文档建议“到 将其视为集合或monad 并使用 map、flatMap、filter 或 foreach”。顶级元素是一个 Option[Any],但是应该是 Some,其中 Map 应该包含单个键“Locations”,应该包含单个键“list”最后是一个列表。在 Scala 中编写检索“id”的函数的惯用方法是什么?

Given a JSON string like this:

{"Locations":
  {"list":
    [
      {"description": "some description", "name": "the name", "id": "dev123"},
      {"description": "other description", "name": "other name", "id": "dev59"}
    ]
  }
}

I'd like to return a list of "id"s from a function parsing the above string. JSON.parseFull() (from scala.util.parsing.json) gives me a result of type Option[Any]. Scala REPL shows it as Some(Map(Locations -> Map(list -> List(Map(id -> dev123, ... and as a beginner in Scala I'm puzzled as to which way to approach it.

Scala API docs suggest "to treat it as a collection or monad and use map, flatMap, filter, or foreach". Top-level element is an Option[Any] however that should be Some with a Map that should contain a single key "Locations", that should contain a single key "list" that finally is a List. What would be an idiomatic way in Scala to write a function retrieving the "id"s?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

会傲 2024-12-16 19:02:44

首先,您应该将 json 从 Any 转换为正确的类型:

val json = anyJson.asInstanceOf[Option[Map[String,List[Map[String,String]]]]]

然后您可以使用 map 方法从 Option 中提取 ids:

val ids = json.map(_("Locations")("list").map(_("id"))).getOrElse(List())

First of all, you should cast json from Any to right type:

val json = anyJson.asInstanceOf[Option[Map[String,List[Map[String,String]]]]]

And then you may extract ids from Option using map method:

val ids = json.map(_("Locations")("list").map(_("id"))).getOrElse(List())
花期渐远 2024-12-16 19:02:44

因为 Any 到处都是返回的结果,所以你必须进行强制转换。使用我之前的答案之一:

class CC[T] { def unapply(a:Any):Option[T] = Some(a.asInstanceOf[T]) }

object M extends CC[Map[String, Any]]
object L extends CC[List[Any]]
object S extends CC[String]
object D extends CC[Double]
object B extends CC[Boolean]

for {
    Some(M(map)) <- List(JSON.parseFull(jsonString))
    M(locMap) = map("Locations")
    L(list) = locMap("list")
    description <- list
    M(desc) = description
    S(id) = desc("id")
} yield id
// res0: List[String] = List(dev123, dev59)

Because Any is everywhere is the returned result, you'll have to cast. Using one of my earlier answers:

class CC[T] { def unapply(a:Any):Option[T] = Some(a.asInstanceOf[T]) }

object M extends CC[Map[String, Any]]
object L extends CC[List[Any]]
object S extends CC[String]
object D extends CC[Double]
object B extends CC[Boolean]

for {
    Some(M(map)) <- List(JSON.parseFull(jsonString))
    M(locMap) = map("Locations")
    L(list) = locMap("list")
    description <- list
    M(desc) = description
    S(id) = desc("id")
} yield id
// res0: List[String] = List(dev123, dev59)
旧话新听 2024-12-16 19:02:44

对于此类任务,您应该查看 Rapture.io。我也是一个 Scala 初学者,但从我搜索的内容来看,这似乎有最友好的语法。这是一个简短的示例,取自 gist

import rapture.io._

// Let's parse some JSON
val src: Json = Json.parse("""
{
  "foo": "Hello world",
  "bar": {
    "baz": 42
  }
}
""")     

// We can now access the value bar.baz
val x: Json = src.bar.baz

// And get it as an integer
val y: Int = x.get[Int]

// Alternatively, we can use an extractor to get the values we want:
val json""" { "bar": { "baz": $x }, "foo": $z }""" = src

// Now x = 42 and z = "Hello world".

For this type of tasks, you should take a look at Rapture.io. I'm also a scala beginner, but from what I've searched for, this seems to have the friendliest syntax. Here's a short example, taken from a gist:

import rapture.io._

// Let's parse some JSON
val src: Json = Json.parse("""
{
  "foo": "Hello world",
  "bar": {
    "baz": 42
  }
}
""")     

// We can now access the value bar.baz
val x: Json = src.bar.baz

// And get it as an integer
val y: Int = x.get[Int]

// Alternatively, we can use an extractor to get the values we want:
val json""" { "bar": { "baz": $x }, "foo": $z }""" = src

// Now x = 42 and z = "Hello world".
流年已逝 2024-12-16 19:02:44

这是你需要的吗? (使用 lift-json)

scala> import net.liftweb.json._
import net.liftweb.json._

scala> implicit val formats = DefaultFormats
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@79e379

scala> val jsonString = """{"Locations":
  {"list":
    [
      {"description": "some description", "name": "the name", "id": "dev123"},
      {"description": "other description", "name": "other name", "id": "dev59"}
    ]
  }
}"""
jsonString: java.lang.String =
{"Locations":
  {"list":
    [
      {"description": "some description", "name": "the name", "id": "dev123"},
      {"description": "other description", "name": "other name", "id": "dev59"}
    ]
  }
}

scala> Serialization.read[Map[String, Map[String, List[Map[String, String]]]]](jsonString)
res43: Map[String,Map[String,List[Map[String,String]]]] = Map(Locations -> Map(list -> List(Map(description -> some desc
ription, name -> the name, id -> dev123), Map(description -> other description, name -> other name, id -> dev59))))

scala> val json = parse(jsonString)
json: net.liftweb.json.package.JValue = JObject(List(JField(Locations,JObject(List(JField(list,JArray(List(JObject(List(
JField(description,JString(some description)), JField(name,JString(the name)), JField(id,JString(dev123)))), JObject(Lis
t(JField(description,JString(other description)), JField(name,JString(other name)), JField(id,JString(dev59))))))))))))

scala> json \\ "id"
res44: net.liftweb.json.JsonAST.JValue = JObject(List(JField(id,JString(dev123)), JField(id,JString(dev59))))

scala> compact(render(res44))
res45: String = {"id":"dev123","id":"dev59"}

Is this what you need? (using lift-json)

scala> import net.liftweb.json._
import net.liftweb.json._

scala> implicit val formats = DefaultFormats
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@79e379

scala> val jsonString = """{"Locations":
  {"list":
    [
      {"description": "some description", "name": "the name", "id": "dev123"},
      {"description": "other description", "name": "other name", "id": "dev59"}
    ]
  }
}"""
jsonString: java.lang.String =
{"Locations":
  {"list":
    [
      {"description": "some description", "name": "the name", "id": "dev123"},
      {"description": "other description", "name": "other name", "id": "dev59"}
    ]
  }
}

scala> Serialization.read[Map[String, Map[String, List[Map[String, String]]]]](jsonString)
res43: Map[String,Map[String,List[Map[String,String]]]] = Map(Locations -> Map(list -> List(Map(description -> some desc
ription, name -> the name, id -> dev123), Map(description -> other description, name -> other name, id -> dev59))))

scala> val json = parse(jsonString)
json: net.liftweb.json.package.JValue = JObject(List(JField(Locations,JObject(List(JField(list,JArray(List(JObject(List(
JField(description,JString(some description)), JField(name,JString(the name)), JField(id,JString(dev123)))), JObject(Lis
t(JField(description,JString(other description)), JField(name,JString(other name)), JField(id,JString(dev59))))))))))))

scala> json \\ "id"
res44: net.liftweb.json.JsonAST.JValue = JObject(List(JField(id,JString(dev123)), JField(id,JString(dev59))))

scala> compact(render(res44))
res45: String = {"id":"dev123","id":"dev59"}
℉服软 2024-12-16 19:02:44

在 JSON 的 SON 分支中,这将起作用。请注意,我没有使用解析器。并不是说它不存在。只是使用构建器方法创建 JSON 对象更容易:

scala> import nl.typeset.sonofjson._
import nl.typeset.sonofjson._

scala> var all = obj(
     |   locations = arr(
     |     obj(description = "foo", id = "807",
     |     obj(description = "bar", id = "23324"
     |   )
     | )

scala> all.locations.map(_.id).as[List[String]]
res2: List[String] = List(23324, 807)

或者使用 a 进行理解:

scala> (for (location <- all.locations) yield location.id).as[List[String]]
res4: List[String] = List(23324, 807)

In a branch of SON of JSON, this will work. Note that I'm not using the parser. Not that it doesn't exist. It's just that creating an JSON object using the builder methods is easier:

scala> import nl.typeset.sonofjson._
import nl.typeset.sonofjson._

scala> var all = obj(
     |   locations = arr(
     |     obj(description = "foo", id = "807",
     |     obj(description = "bar", id = "23324"
     |   )
     | )

scala> all.locations.map(_.id).as[List[String]]
res2: List[String] = List(23324, 807)

Or use a for comprehension:

scala> (for (location <- all.locations) yield location.id).as[List[String]]
res4: List[String] = List(23324, 807)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文