2

我想知道是否有人可以给出一个对使用“anyOf”关键字的模式有效的 JSON 示例?我正在尝试编写一些将在 python 中输出有效 JSON 的代码。

例如,如果您有一个如下所示的架构:

"anyOf": {
       "category_0": {
                "main": { "enum": ["learning"]},
                "sub":  { "enum": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]}
                         },
       "category_1": {
                "main": { "enum": ["mental"]},
                "sub":  { "enum": ["down's syndrome", "asperger's syndrome", "autism", "other"]}
                 },
       "category_2": {
                "main": { "enum": ["behavioral"]}
                 },
       "category_3": {
                "main": { "enum": ["medical"]},
                "sub":  { "enum": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]}
                 },
       "category_4": {
                "main": { "enum": ["physical"]},
                "sub":  { "enum": ["blind", "deaf", "cerebral palsy", "other"]}
                    }
                 }

什么 JSON 是有效的?

我用来在 python 中输出 JSON 的代码是这样的:

main = row.get('main', [])
sub = row.get('sub',[])
category = dict(main = main, sub=sub)
special_needs = category
4

1 回答 1

3

您的 JSON 无效,因为字符串不能位于最外层的数组或对象之外。

最低限度有效的 JSON 如下所示:{}. 您可以通过将您在此处显示的内容放入其中来使您的 JSON 有效:

{
  "anyOf": {
    "category_0": {
      "main": {
        "enum": [
          "learning"
        ]
      },
      "sub": {
        "enum": [
          "ADD/ADHD",
          "dyslexia",
          "general learning disability",
          "language disorder",
          "intellectual giftedness",
          "other"
        ]
      }
    },
    "category_1": {
      "main": {
        "enum": [
          "mental"
        ]
      },
      "sub": {
        "enum": [
          "down's syndrome",
          "asperger's syndrome",
          "autism",
          "other"
        ]
      }
    },
    "category_2": {
      "main": {
        "enum": [
          "behavioral"
        ]
      }
    },
    "category_3": {
      "main": {
        "enum": [
          "medical"
        ]
      },
      "sub": {
        "enum": [
          "diabetes",
          "allergies",
          "eating disorders",
          "chronic illness",
          "other"
        ]
      }
    },
    "category_4": {
      "main": {
        "enum": [
          "physical"
        ]
      },
      "sub": {
        "enum": [
          "blind",
          "deaf",
          "cerebral palsy",
          "other"
        ]
      }
    }
  }
}
于 2013-07-19T21:17:42.160 回答