简体   繁体   English

在JavaScript中动态创建JSON对象

[英]Creating a JSON object in JavaScript dynamically

I need to create a JSON object dynamically in JavaScript using a for loop. 我需要使用for循环在JavaScript中动态创建JSON对象。 I have tried using the array.Push method but it is not working. 我已经尝试使用array.Push方法,但是它不起作用。 I am only getting the first value getting stored. 我只得到第一个值被存储。 The remaining values of the iteration are not getting stored. 迭代的剩余值不会被存储。

This is what I am trying: 这是我正在尝试的:

var array = [];
    for (var i = 0; i < 4; i++) {
        var username = drlist.reportees[i].name;
        var think40 = getthink40(n,m);
        if (think40.isSuccessful){
            var result = think40.array;
            var length = result.length;
            var tes= 0;
            for (var j = 0; j < length-1; j++){
                tes = tes + parseFloat(result[j].duration);
            }
            var hours = tes/60;
            var think = (hours/40)*100;
            if (think > 100)
                {
                think =100;
                }
            array.push( {
                    name: username,
                    hours: think
                });
                }
        return array;
    }

Try this... 尝试这个...

var jsonArray = [];
function test (){
for (var i=0; i<3;i++)
{
   var jsonObject = {'a':1, 'b':2}; jsonArray.push(jsonObject);
}
return jsonArray;
}

Note : You are returning the jsonObject instead of jsonArray. 注意:您将返回jsonObject而不是jsonArray。 You should probably return the jsonArray. 您可能应该返回jsonArray。

Creating and manipulating a JSON object in JavaScript is not very different from any other type of object, but does have a few limitations. 在JavaScript中创建和操作JSON对象与其他任何类型的对象都没有太大区别,但是确实有一些限制。 These are primarily around the data types JSON supports and the two most notable are a lack of dates and functions. 这些主要围绕JSON支持的数据类型,最值得注意的两个是缺少日期和功能。

JavaScript objects can contain almost anything (increasingly so with ES6 and symbols) but JSON is a limited subset of that. JavaScript对象可以包含几乎所有内容(ES6和符号越来越多),但是JSON是其中的有限子集。 The JSON spec is short and easy to read (complete with pictures!), so I would recommend starting there. JSON规范简短易读(附带图片!),因此我建议从此处开始。 As you'll see in the spec, the value types include strings, numbers, objects, array, boolean keywords, and null. 正如您将在规范中看到的那样,值类型包括字符串,数字,对象,数组,布尔关键字和null。 There is no support for dates -- easily worked around by formatting them as ISO 8601 strings -- or functions. 不支持日期-通过将日期格式设置为ISO 8601字符串或功能可以轻松解决。

To turn a valid JavaScript object into the final JSON form involves a limited amount of string formatting and escaping. 要将有效的JavaScript对象转换为最终的JSON格式,需要进行少量的字符串格式化和转义。 Most modern browsers have a global JSON API for doing that, providing both parse and render ( stringify ) methods. 大多数现代浏览器都具有全局JSON API来执行此操作,同时提供了parse和render( stringify )方法。 This is your codec and the first and final step when manipulating JSON. 这是您的编解码器 ,也是处理JSON的第一步和最后一步。

In your example, you would build the object as normal, assigning properties ( foo.bar = 3 ) as necessary. 在您的示例中,您将正常构建对象,并根据需要分配属性( foo.bar = 3 )。 At the very end, to return the JSON (which is really just a string in JS), you would take the object you've created and pass it to JSON.stringify . 最后,要返回JSON(实际上只是JS中的一个字符串),您可以将创建的对象传递给JSON.stringify This will produce a valid, safe, escaped JSON string suitable for passing to web services and other scripts. 这将产生一个有效,安全,转义的JSON字符串,适合传递给Web服务和其他脚本。

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

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