[英]How to read json with the given structure on scala play?
I have a json list structure like this, how can we read this structure and convert in a list in scala, play framwork. 我有一个像这样的json列表结构,我们如何才能读取此结构并在scala中转换为列表,播放框架。
{
"0":{
"name":"Foo",
"age":10
},
"12":{
"name":"Bar",
"age":10
}
.
.
.
}
If, there were json structure like below, I could have done it easily, but since I am not that versed in parsing Json and actually new to scala too, I couldn't understand how to work with Json strucutures that have key as "0","12" like in the first example. 如果有如下所示的json结构,我可以轻松完成,但是由于我并不精于解析Json,而且实际上也不是scala的新手,所以我不明白如何使用键为“ 0”的Json结构如第一个示例中的“,” 12“。 I had been using Jerkson wrapper for Jackson for generating/parsing json/case-class.
我一直在为杰克逊使用Jerkson包装器来生成/解析json / case-class。
{
{"name":"Foo","age":10},
{"name":"Bar",age:11}
}
Update: 更新:
I actually succeeded in parsing the json structure, in addition to Ryans answer, 实际上,我成功地解析了json结构,除了Ryans的答案,
import play.api.libs.json._
case class Doodad(name: String, age: Int)
object Doodad {
implicit val fmt = Json.format[Doodad]
def parserFuntion = {
var myJsValue: JsValue = Json.parse(jsonString)// added line in the ans
var requiredMap = myJsValue.validate[Map[String, Doodad]]
}
}
But now I have a new questions, the json structure I working with seems to be a bit irregular for eg: 但是现在我有一个新问题,例如,我使用的json结构似乎有点不规则:
{
"0":{
"name":"Foo",
"age":10
},
"12":{
"name":"Bar",
"age":10
},
"13":{
"name":"Foobar"
"age":20,
"meta":{
"desc":"Irregular Json",
"img":"Some Link"
}
},
.
.
.
}
Chances are some of the json list elements might have that extra "meta" key->value pair, and some might not. 某些json列表元素可能具有额外的“元”键->值对,而某些可能没有。 So, how can we deal with such situations.
因此,我们如何应对这种情况。 Any ideas??
有任何想法吗??
case class Doodad(name: String, age: Int)
object Doodad {
implicit val fmt = Json.format[Doodad]
}
myJsValue.validate[Map[String, Doodad]] // JsResult[Map[String, Doodad]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.