JavaScript - JSON

JSON 数据

可以将JSON对象直接赋值给指定的变量

let data = {
    "status": "success",
    "type": "new",
    "list1": [{
        "name": "投标保函申请",
        "x": 300,
        "y": 100,
        "value": 58249,
        "userName": "",
        "comment": "",
        "itemStyle": {"borderColor": "#24a689"}
    }],
    "list2": [{"target": 58249, "source": null, "lineStyle": {"color": "#24a689"}}]
};

JSON对象没有length属性,如果JSON对象的子对象是个数组,那么这个数组是有长度的。

var json1 = {"abc": [{"name": "txt1"}, {"name": "txt2"}]};
for (var i = 0; i < json1.abc.length; i++) {
    alert(json1.abc[i].name);
}

// 遍历JSON
var json2 = {"name1": "txt1", "name2": "txt2"};
for (var js2 in json2) {
    alert(js2 + "=" + json2[js2]);
}

// 计算JSON长度
function getJsonLength(jsonData) {
    var jsonLength = 0;
    for (var item in jsonData) {
        jsonLength++;
    }
    return jsonLength;
}
posted @ 2021-02-01 13:30  duchaoqun  阅读(60)  评论(0编辑  收藏  举报