【问题标题】:JSON Schema Parsing to get column names as per the schema orderJSON Schema Parsing 以根据模式顺序获取列名
【发布时间】:2018-11-16 18:54:47
【问题描述】:

我从 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 config schema 单独获取数据文件。

【问题讨论】:

    标签: json scala parsing schema


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 2020-08-07
      • 2021-06-09
      • 1970-01-01
      • 2012-02-18
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多