简体   繁体   English

如何为包含Properties对象的对象定义JSON模式?

[英]How to define JSON schema for object that holds Properties object?

I need to create a JSON schema for object that will include java Properties object as one of its properties. 我需要为对象创建一个JSON模式,该模式将包含java Properties对象作为其属性之一。 The nested Properties object will be simply list of key=value. 嵌套的Properties对象将只是key = value的列表。 Both key and value are of type string. 键和值都是string类型。 I failed to find any docs that describe how to define the schema that includes 2 new types. 我找不到任何描述如何定义包含2种新类型的模式的文档。

shall it be something like: 应该是这样的:

{
"type": "object",
"name": "MyObj",
"properties": {
    "prop1": {
        "type": "string",
        "description": "prop1",
        "required": true
    },
    "props": {
        "type": "array",
        "items": {
            "type": "object"
            "properties": {
                "key": {
                    "type": "string",
                    "description": "key",
                    "required": true
                },
                "value": {
                    "type": "string",
                    "description": "the value",
                    "required": true
                }
            }
            "description": "the value",
            "required": true
        }
    }
}

} }

The schema you have written (assuming the commas are fixed) describes data of the form: 您编写的模式(假设逗号已修复)描述了表单的数据:

{
    "prop1": "Some string property goes here",
    "props": [
        {"key": "foo", "value": "bar"},
        {"key": "foo2", "value": "bar2"},
        ...
    ]
}

If this is what you wanted, then you are already finished. 如果这是你想要的,那么你已经完成了。

However, I do wonder why you are using key/value pairs in an array, when you could use a JSON object with string keys instead. 但是,我确实想知道为什么你在数组中使用键/值对,当你可以使用带有字符串键的JSON对象时。 Using the additionalProperties keyword, you could have a schema: 使用additionalProperties关键字,您可以拥有一个模式:

{
    "type": "object",
    "name": "MyObj",
    "properties": {
        "prop1": {
            "type": "string",
            "description": "prop1"
        },
        "props": {
            "type": "object",
            "additionalProperties": {
                "type": "string",
                "description": "string values"
            }
        }
    }
}

This describes a data format like: 这描述了一种数据格式,如:

{
    "prop1": "Some string property goes here",
    "props": {
        "foo": "bar",
        "foo2": "bar2"
    }
}

At W3 schools (JSON Syntax) you can read how the array should be defined. W3学校(JSON语法),您可以阅读如何定义数组。

There is no schema like the xsd for xml, however i've found an approach on json-schema.org . 没有类似xsd for xml的架构,但是我在json-schema.org上找到了一种方法。 If you are able to, i'll advice to youse google-GSON library for JSON . 如果你能够,我会建议你使用google-GSON 库获取JSON You could Store key Value as "id" : "value" and build only one object, containing all requieed pairs: 您可以将键值存储为"id" : "value"并仅构建一个包含所有必需对的对象:

{ "lang" : "EN" , "color" : "red" }

Your posted model is incorect, you can check it on jsonlint.com Here is a working version, i'm not sure if the modell is as expected. 你发布的模型是incorect,你可以在jsonlint.com上查看这是一个工作版本,我不确定modell是否符合预期。

{
    "type": "object",
    "name": "MyObj",
    "properties": [
        {
            "prop1": {
                "type": "string",
                "description": "prop1",
                "required": true
            },
            "props": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "key": {
                            "type": "string",
                            "description": "key",
                            "required": true
                        },
                        "value": {
                            "type": "string",
                            "description": "the value",
                            "required": true
                        }
                    },
                    "description": "the value",
                    "required": true
                }
            }
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM