【问题标题】:Array of objects using IODOCS使用 IODOCS 的对象数组
【发布时间】:2015-08-17 07:05:31
【问题描述】:

我想添加一个对象数组,比如

    "identifiers": [
      {
        "primary": true
       },
       {
        "primary": false
       },
     ]

但是数组里面的对象总是需要一个名字来创建像

    "identifiers": [
       {
         "IDENTIFIER": {
             "primary": true
         }
       }
     ]

我在配置文件中使用了以下 JSON 代码,

    "identifiers": {
        "title": "Identifiers",
        "type": "array",
        "location": "body",
        "items": {
            "title": "Identifier Fields",
            "type": "object",
            "properties": {
                "IDENTIFIER": {
                    "type": "object",
                    "properties": {
                        "primary": {
                            "title": "primary",
                            "required": true,
                            "type": "boolean",
                            "description": "",
                            "default": true
                        }
                    }
                }
            }
        }
    }

如何实现这一点。请帮助我。

提前致谢。

【问题讨论】:

    标签: rest iodocs mashery


    【解决方案1】:

    你可以这样做

    data =  {"identifiers": [
          {
            "primary": true
           },
           {
            "primary": false
           },
         ]};
    
    for(i=0;i<data.identifiers.length;i++) {
        obj = data.identifiers[i];
        obj =  {
             "IDENTIFIER": {
                 "primary": obj.primary
             }
           }
    

    //如果你想要 obj 的所有属性意味着你有循环通过它创建它们。通过这个copying properties }

    【讨论】:

      【解决方案2】:

      感谢您的回复。

      我通过添加额外的对象参数犯了一个错误。我已经通过删除额外的对象来修复它,例如

      "identifiers": {
          "title": "Identifiers",
          "type": "array",
          "location": "body",
          "items": {
              "title": "Identifier Fields",
              "type": "object",
                      "properties": {
                          "primary": {
                              "title": "primary",
                              "required": true,
                              "type": "boolean",
                              "description": "",
                              "default": true
                          }
                      }
                  }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-29
        • 2013-04-22
        • 2016-04-29
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        相关资源
        最近更新 更多