数组对象转json格式
生活随笔
收集整理的這篇文章主要介紹了
数组对象转json格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、數(shù)組轉化成JSON對象后,key值是索引,value是數(shù)組對應的值。
//數(shù)組也可以轉化成JSON對象var jStr3 = "[[10,20,30],40,50,60]";var j3 = JSON.parse(jStr3);for(let key in j3){console.log('key:',key);} // key: 0 // key: 1 // key: 2 // key: 3for(let value of j3){console.log('value:',value);} // value: (3) [10, 20, 30] // value: 40 // value: 50 // value: 60j3.forEach((item,index)=>{console.log('item:',item,'index:',index);}) // item: (3) [10, 20, 30] index: 0 // item: 40 index: 1 // item: 50 index: 2 // item: 60 index: 3j3 = JSON.parse(jStr3,(key,value)=>{console.log('key:',key,'value:',value);}); // 把所有值都遍歷出來了 // key: 0 value: 10 // key: 1 value: 20 // key: 2 value: 30 // key: 0 value: (3) [empty × 3] // key: 1 value: 40 // key: 2 value: 50 // key: 3 value: 60 // key: value: (4) [empty × 4]2、數(shù)組對象可以直接序列化成字符串
var jStr31 = [[10,20,30],40,50,60];console.log(JSON.stringify(jStr31));console.log(jStr31.toString());console.log(jStr31.join('-')); // [[10,20,30],40,50,60] // 10,20,30,40,50,60 // 10,20,30-40-50-603、對象數(shù)組轉化成JSON對象
var jStr = '[{"name":"a"},{"name":"b"}]'; var j = JSON.parse(jStr); console.log(j); // (2) [{…}, {…}] // 0: {name: "a"} // 1: {name: "b"} // length: 2 // __proto__: Array(0)for(let key in j){console.log('key:',key) } // key: 0 // key: 1for(let item of j){console.log('item of:',item.name); } // item of: {name: "a"} // item of: {name: "b"}j.forEach((item,index)=>{console.log('index:',index,'item:',item);}) // index: 0 item: {name: "a"} // index: 1 item: {name: "b"} ---------------------原文:https://blog.csdn.net/ForMyQianDuan/article/details/78328487
總結
以上是生活随笔為你收集整理的数组对象转json格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gis怎么提取水系_ArcGIS水文分析
- 下一篇: POJ分类-转载