如何创建具有多个属性/属性的单个JSON对象?

时间:2014-05-20 08:52:58

标签: javascript json

我需要创建一个名为args的JSON对象,其中包含2个属性/属性,key1和key2。

目前这就是我所拥有的,但它没有被正确解析...

var args:{
   'key1': $scope.args.users, 
   'key2': 'http://financialsystems.sungard.com/'
}

其中$ scope.args.users是一个数组:

$scope.args.users = [
        {id: '1', firstName: 'Geraldine', lastName: 'Roberts', email: 'geraldine.roberts@email.com', country: 'South Africa'},
        {id: '2', firstName: 'Walter', lastName: 'Mitty', email: 'w.mitty@email.com', country: 'USA'}
    ];

我哪里错了?每个属性/属性周围是否应该有另一组括号?

2 个答案:

答案 0 :(得分:1)

  1. 您无法使用标签启动JSON对象。看起来你错过了{}在你身边
  2. JSON中的属性名称必须是字符串。 args需要"args"
  3. 字符串必须引用"而不是''key1'需要"key1"
  4. 您无法在所有
  5. 中使用JSON中的变量

    这样:

    {
        "args": {
            "key1": [
                {
                    "id": "1",
                    "firstName": "Geraldine",
                    "lastName": "Roberts",
                    "email": "geraldine.roberts@email.com",
                    "country": "South Africa"
                },
                {
                    "id": "2",
                    "firstName": "Walter",
                    "lastName": "Mitty",
                    "email": "w.mitty@email.com",
                    "country": "USA"
                }
            ],
            "key2": "http://financialsystems.sungard.com/"
        }
    }
    

答案 1 :(得分:0)

:替换为=

 var args = {
   'key1': $scope.args.users, 
   'key2': 'http://financialsystems.sungard.com/'
  };