js中json数据的读取

来源:互联网 发布:android 实时监听网络 编辑:程序博客网 时间:2024/05/21 20:29

For...In 声明用于对数组或者对象的属性进行循环/迭代操作。



var data=[{"clothesID1":{"clothesID":1,"name":"上衣""remark":"上衣第一层"},"clothesID2":{"clothesID":2,"name":"下衣","remark":"下衣第一层"}}]  //数据自行修正

遍历data的所有属性

//var clothesTypePros = [];
for(var p in data){ //属性为clothesID1,clothesID2
    //clothesTypePros.push(p);
}

//属性只有0

for(var p in data[0]){ //属性为clothesID1,clothesID2
    //clothesTypePros.push(p);
}

//属性有clothesID1,和clothesID2


当继续遍历则有

for(var p in data[0]["clothesID1"]){ //属性为clothesID1,clothesID2
    //clothesTypePros.push(p);
}

//则有{"clothesID1":{"clothesID":1,"name":"上衣""remark":"上衣第一层"}的属性 clothesID、name、remark

如果取值,只需 data[0]["clothesID1"].name                     data[0]["clothesID1"].remark等