我有这个向量:
val imageIds = Vector(
"XXXX1",
"XXXX2",
"XXXX3"
)
我目前使用以下方法创建一个数组:
def makeTheImageDataArray: Vector[JsObject] = {
imageIds.map(SingleImageData(_, "theURL", "theStatus").asJsObject)
}
具有此案例类:
case class SingleImageData(ImageId: String, URL: String, Status: String) {
def imageId: String = ImageId
def getURL: String = URL
def status: String = Status
def asJsObject: JsObject = JsObject(
"ImageId" -> JsString(imageId),
"URL" -> JsString(getURL),
"Status" -> JsString(status)
)
}
哪个会产生:
Vector(
{"ImageId":"XXXX1","URL":"theURL","Status":"theStatus"},
{"ImageId":"XXXX2","URL":"theURL","Status":"theStatus"},
{"ImageId":"XXXX3","URL":"theURL","Status":"theStatus"}
)
我不想创建一个Vector,而是要创建一个HashMap,以ImageId作为键,即:
Map(
XXX1 -> {"URL":"theURL","Status":"theStatus"},
XXX2 -> {"URL":"theURL","Status":"theStatus"},
XXX3 -> {"URL":"theURL","Status":"theStatus"}
)
谁能告诉我该怎么做?
答案 0 :(得分:2)
从"ImageId" -> JsString(imageId)
中删除asJsObject
,然后
imageIds.map(id => id -> SingleImageData(id, "theURL", "theStatus").asJsObject).toMap
或者如果SingleImageData不需要知道ID,则从SingleImageData中完全删除ImageID,然后
imageIds.map(_ -> SingleImageData("theURL", "theStatus").asJsObject).toMap