如何使用spray.json解析empty []

时间:2018-04-29 17:32:31

标签: json scala spray-json

我有以下Json:

{
    "my_rule":[
      {
        "labelField": "abc",
        "nodeAttr":[]
      },
      {
        "labelField": "def",
        "nodeAttr":["name","surname"]
      }
    ]
}

我使用spray.json在Scala中解析它:

my_map = parsedJson.map(rule =>
  Map(
    "label" -> rule.labelField,
    "attr" -> rule.nodeAttr(???) // TODO
  )
)

attr的类型为Seq[String]

我不知道如何解决空nodeAttr的情况?目标是让attr等于"""name,surname"

1 个答案:

答案 0 :(得分:1)

my_map = parsedJson.map(rule =>
  Map(
    "label" -> rule.labelField,
    "attr" -> (if(rule.nodeAttr.size == 0 ) "" else rule.nodeAttr.mkString(","))
  )
)