简体   繁体   English

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

[英]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. 我需要创建一个名为args的JSON对象,它具有2个属性/属性,key1和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是一个数组:

$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. 您不能使用标签启动JSON对象。 It looks like you are missing {} around yours 看来您周围缺少{}
  2. Property names in JSON must be strings. JSON中的属性名称必须是字符串。 args needs to be "args" args必须是"args"
  3. Strings must be quoted with " not ' . 'key1' needs to be "key1" , etc 字符串必须用" not '引号'key1'必须为"key1""key1"
  4. You can't use variables in JSON at all 您根本无法在JSON中使用变量

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/'
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个方法来映射我的 JSON 对象以在 vue.js 中设置数据对象的属性? - How do I create a methods that map my JSON object to set properties of data object in vue.js? 如何遍历json对象的嵌套属性并创建新的数组列表? - How do I iterate through nested properties of an json object and create a new array list? 如何在JavaScript中创建具有对象属性的对象类? - How do I create an object class with object properties in JavaScript? 在Java中,如何在单个JSON对象中表示多个(相同类型的)对象 - In Java, How do I represent multiple objects (of same type) in a single JSON object 如何创建单个对象并在其中推送多个密钥对值? - How do I create a single object and push multiple key-pair values inside? 使用 lodash,如何在 object 上设置多个属性? - Using lodash, how do I set multiple properties on an object? 如何通过JSON和JavaScript访问单个对象? - How do I access a single object from JSON wih javascript? 如何使用循环创建多个对象属性 - How to create multiple object properties with a loop 如何创建具有多个 AJAX 属性的 javascript 对象? - How to create a javascript object with multiple AJAX properties? 如何将JSON序列化的对象转换为JS对象,以便可以访问其属性? - How do I convert a JSON serialized object into a JS object, so I can access its properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM