JSON.parse 解析json 上有問題嗎 ??
代碼 : var old_m, new_m, oldArr, newArr;
console.log(“TEST A”);
old_m = '[{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"}]'; oldArr = JSON.parse( old_m );
new_m = '{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"}’; newArr = JSON.parse( new_m );
console.log(“old_text:” + old_m ); console.log(“old Arr:” + oldArr.date );
console.log(“new_text:” + new_m ); console.log(“new Arr:” + newArr.date );
console.log(“TEST B”);
old_m = '[{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"},{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"}]'; oldArr = JSON.parse( old_m );
new_m = '{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"},{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"}’; newArr = JSON.parse( new_m );
console.log(“old_text:” + old_m ); console.log(“old Arr:” + oldArr.date );
console.log(“new_text:” + new_m ); console.log(“new Arr:” + newArr.date );
============================== OUTPUT :
TEST A
old_text:[{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"}] old Arr:undefined
new_text:{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"} new Arr:20-06 11:23:01
TEST B
old_text:[{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"},{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"}] old Arr:undefined
new_text:{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"},{"date":"20-06 11:23:01","_id":"51c2759fa828f0920b000005"} new Arr: 已死