python - 如何要求 JSON 列表中的数据?
问题描述
如何需要列表中的数据?如何做到这一点并指定类型,例如字典?
下面是两个 JSON 示例和一个模式。根据架构,这两个 JSON 样本都是有效的。具有空列表的示例应该无法验证恕我直言。我该如何做到这一点?
from jsonschema import validate
# this is ok per the schema
{
"mylist":[
{
"num_items":8,
"freq":8.5,
"other":2
},
{
"num_items":8,
"freq":8.5,
"other":4
}
]
}
# this should fail validation, but does not.
{
"mylist":[
]
}
# schema
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"mylist": {
"type": "array",
"items": {
"type": "object",
"properties": {
"num_items": {
"type": "integer"
},
"freq": {
"type": "number"
},
"other": {
"type": "integer"
}
},
"required": [
"freq",
"num_items",
"other"
]
}
}
},
"required": [
"mylist"
]
}
解决方案
推荐阅读
- azure - 重命名 Azure DevOps 项目
- javascript - 如何将 Firestore 时间戳转换为 Date() - Firestore V9
- matlab - Matlab中自回归神经网络的多个输入
- reactjs - 如何通过单击datagrid中的单元格正确删除datagrid中的列?
- javascript - 如何旋转和定位中心对象?
- swift - 我可以使用 Swift async/await 来连续调用 void 方法吗?
- java - 无法使用 Java 发送 Whatsapp 消息
- python - 熊猫 - 计算每小时平均滚动 10 分钟
- python - `numba` 和 `numpy.concatenate`
- r - 为什么我不能对 z_scores 使用箱线图?