JSON Schema解析以根据架构顺序获取列名

时间:2018-06-07 00:25:19

标签: json scala parsing schema

我从Json架构(属性)中提取字段,但字段不是按照架构顺序。

代码: - JSon Schema文件 -

{
  "type": "object",
  "required": [ "title", "description" ],
  "properties": {
    "title": { "type": "string" },
    "description": { "type": "string" },
    "termsOfServiceUrl": { "type": "string", "format": "uri" },
    "contact": { "type": "integer", "format": "email" },
    "license": { "type": "string" },
    "licenseUrl": { "type": "string", "format": "uri" }
  },
  "additionalProperties": false
}

val schemafileparse = parse(schemafile)

 val column_list = (schemafileparse \"properties").camelizeKeys.extract[Map[String,Any]].keySet

结果: -

Set(termsOfServiceUrl, description, contact, license, title, licenseUrl)

预期结果: -

Set(title, description, termsOfServiceUrl, contact, license, licenseUrl)

我怎样才能得到预期的结果。我将根据json配置架构单独获取数据文件。

1 个答案:

答案 0 :(得分:0)

由于JObject默认使用HashMap获取 JObject 值, HashMap 无序

所以也许您可以手动从JObject收集字段名称,例如:

  val column_list = (schemafileparse \ "properties") match {
    case JObject(k) => k.map(i => i._1)
  }