JSON对象的长度 [英] Length of a JSON object

查看:59
本文介绍了JSON对象的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数正在生成一个带有json对象的数组:

This function is generating a array with json objects on it:

var estoque={};
function unpack_estoque(tnm,total,estoque_vl,id,tid,st) {
    tnm=tnm.split("|");
    total=total.split("|");
    estoque_vl=estoque_vl.split("|");
    id=typeof id != 'undefined' ? id.split("|") : null;
    tid=typeof tid != 'undefined' ? tid.split("|") : null;
    st=typeof st != 'undefined' ? st.split("|") : null;

    for (var i in tnm)
        estoque[i]={
            "id": id != null ? id[i] : null,
            "tid": tid != null ? tid[i] : null,
            "total": total[i],
            "estoque": estoque_vl[i],
            "tnm": tnm[i],
            "st": st != null ? st[i] : null
        };
}

现在如何获取estoque长度来遍历收集的项目?

Now how do i get the estoque length to loop through the collected items?

estoque.length返回undefined,而for (var i in estoque)遍历JSON对象而不是estoque []根数组.

estoque.length returns undefined while for (var i in estoque) loops through JSON object and not estoque[] root array.

谢谢.

推荐答案

var estoque = {}; //object estoque.length will not work (length is an array function)
var estoque = new Array(); //estoque.length will work

您可以尝试:

 var estoque = new Array();
 for (var i in tnm)
    estoque.push({
        "id": id != null ? id[i] : null,
        "tid": tid != null ? tid[i] : null,
        "total": total[i],
        "estoque": estoque_vl[i],
        "tnm": tnm[i],
        "st": st != null ? st[i] : null
    });

现在estoque将是一个数组(您可以将其作为一个数组遍历).

Now estoque will be an array (which you can traverse as one).

这篇关于JSON对象的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆