日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

数组对象转json格式[通俗易懂]

發(fā)布時(shí)間:2023/12/24 综合教程 35 生活家
生活随笔 收集整理的這篇文章主要介紹了 数组对象转json格式[通俗易懂] 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、數(shù)組轉(zhuǎn)化成JSON對(duì)象后,key值是索引,value是數(shù)組對(duì)應(yīng)的值。

//數(shù)組也可以轉(zhuǎn)化成JSON對(duì)象
    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: 3

    for(let value of j3){
        console.log('value:',value);
    }
//    value: (3) [10, 20, 30]
//    value: 40
//    value: 50
//    value: 60

    j3.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: 3

    j3 = JSON.parse(jStr3,(key,value)=>{
        console.log('key:',key,'value:',value);
    });
// 把所有值都遍歷出來(lái)了
//    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ù)組對(duì)象可以直接序列化成字符串

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-60

3、對(duì)象數(shù)組轉(zhuǎn)化成JSON對(duì)象

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: 1


for(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

總結(jié)

以上是生活随笔為你收集整理的数组对象转json格式[通俗易懂]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。