如何为映射定义JSON模式<;字符串,整数>;?
我有一个json:如何为映射定义JSON模式<;字符串,整数>;?,json,schema,geojson,jsonschema,json-schema-validator,Json,Schema,Geojson,Jsonschema,Json Schema Validator,我有一个json: { "itemTypes": {"food":22,"electrical":2}, "itemCounts":{"NA":211} } 在这里,ItemType和itemCounts将是通用的,但其中的值(食物、NA、电气)将不断变化,但其格式为:Map 如何为这种通用结构定义Json模式 我试过: "itemCounts":{ "
{
"itemTypes": {"food":22,"electrical":2},
"itemCounts":{"NA":211}
}
在这里,ItemType和itemCounts将是通用的,但其中的值(食物、NA、电气)将不断变化,但其格式为:Map
如何为这种通用结构定义Json模式
我试过:
"itemCounts":{
"type": "object"
"additionalProperties": {"string", "integer"}
}
你可以:
{
"type": "object",
"properties": {
"itemType": {"$ref": "#/definitions/mapInt"},
"itemCount": {"$ref": "#/definitions/mapInt"}
},
"definitions": {
"mapInt": {
"type": "object",
"additionalProperties": {"type": "integer"}
}
}
}
通过定义json shcema,您是否在询问如何编写一个表示json的类?@J.West没有JSONI形式的模式?这是您想要参考的吗?我们如何指定这两个模式中的哪一个应该在这里被视为“键”?键在json中总是
string
s。本例中的itemType
和itemCount
都是“字符串”到“整数”的映射。还要注意的是,要将字符串映射到int,不必使用定义
这件事——这只是本例中用于消除重复定义的快捷方式。我正在搜索如何表示映射
。我控制服务器和客户端,因此也控制序列化程序,但不知道如何将密钥定义为数字,有什么帮助吗?@ttugates您将无法在JSON模式中这样做,因为它不符合JSON规范
{
"type": "array",
"maxItems": 125,
"items": {
"type": "array",
"items": [
{ // key schema goes here },
{ // value schema goes here }
],
"additionalItems": false
}
}