简体   繁体   中英

How do I create a single JSON object with multiple attributes/properties?

I need to create a single JSON object called args with 2 attributes/property, key1 and key2.

Currently this is what I have but it is not being parsed correctly...

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

where $scope.args.users is an array:

$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'}
    ];

Where am I going wrong? Should there be another set of braces around each attribute/property?

  1. You can't start a JSON object with a label. It looks like you are missing {} around yours
  2. Property names in JSON must be strings. args needs to be "args"
  3. Strings must be quoted with " not ' . 'key1' needs to be "key1" , etc
  4. You can't use variables in JSON at all

Such:

{
    "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/"
    }
}

Replace : to =

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM