【问题标题】:JavaScript formatting json value depending schemaJavaScript 格式化 json 值取决于模式
【发布时间】:2017-07-11 14:18:47
【问题描述】:

是否有一种简单的方法可以根据其 JSON Schema 上的格式(特别是 mongodb 的日期/日期时间格式)来获取 JSON 字段的值? 例子: JSON:

{
  "firstName": "Alex",
  "lastName": "Alex",
  "birthDate": "1996-06-20"
}

架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {},
    "id": "http://example.com/example.json",
    "properties": {
        "birthDate": {
            "id": "/properties/birthDate",
            "type": "string",
              "format":"date"
        },
        "firstName": {
            "id": "/properties/firstName",
            "type": "string"
        },
        "lastName": {
            "id": "/properties/lastName",
            "type": "string"
        }
    },
    "type": "object"
}

转换后的最终结果:

{
  "firstName": "Alex",
  "lastName": "Alex",
  "birthDate": {
    $date": 835228800000
  }
}

(我使用这种格式是因为我使用的是 RestHeart)

在日期时间的情况下做同样的事情。 这种格式必须是通用的并且对嵌套对象也有效。

感谢您的帮助

【问题讨论】:

    标签: javascript json mongodb jsonschema mongodate


    【解决方案1】:

    我开发了一个 Node.js 模块来解决这个问题: 以下是感兴趣的人的链接: https://www.npmjs.com/package/mongodates

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      相关资源
      最近更新 更多