根据需求定义JSON

时间:2014-01-03 09:09:01

标签: javascript json

我需要为以下要求定义JSON;

Company company : Object (Name, Id)
User user – Object (Name, Age)
List<CustomObj> serviceList  
    o   Service service
    o   String mode
    o   Boolean flag1
    o   Boolean flag2

我尝试了类似的事情;

{
  Company : { "name" : "Company1", "id" : 123 },
  User : {"name" : ["PV","PR", "DM", "TN"], "age" : null},
  serviceList : 
  {
  Service: {},
  "mode" : null,
  "flag1" : null;
  "flag2" : null
  }
}   

我在几个地方有空值的原因是因为有两种模式;创建和编辑..

因此,在“创建”模式下,某些字段将具有空值...

但是如果它是正确的你可以让我知道吗?

1 个答案:

答案 0 :(得分:1)

以下是您编辑前的答案,请参阅下面的后续内容:

company看起来像一个简单的对象:

{"Name": "the name", "Id": "the ID"}

user一样:

{"Name": "the name", "Age": "the age"}

serviceList看起来像一个对象数组:

[
    {
     "service": {},
     "mode": "the mode",
     "flag1": true,
     "flag2": false
    },
    ...
]

您尚未显示Service应该是什么样子,所以我刚刚使用了上面的{};它看起来像是一个物体。 ...中的serviceList不是字面值,而是表示您可能会重复上面的内容。

将所有这些内容整合到一个JSON文档中:

{
    "company":     {"Name": "the name", "Id": "the ID"},
    "user":        {"Name": "the name", "Age": "the age"},
    "serviceList": [
        {
         "service": {},
         "mode": "the mode",
         "flag1": true,
         "flag2": false
        },
        ...
    ]
}

同样,..中的serviceList不是文字,它表示您可能会重复上面的对象。


您修改了问题以添加

  

我尝试了类似的事情;

以下。从我原来的答案可以看出,我会说serviceList将是一个数组,而不是一个对象,因为List通常意味着:一个东西的列表,在JSON中将是数组。您还使用了小写标识符,无论您引用的是什么,您必须复制的是使用最初限制的标识符。 JSON区分大小写,因此可能会出现问题。