zoukankan      html  css  js  c++  java
  • json 拼接多个对象

    var json = {};

    var json1 = {a:1,b:1};
    var json2 = {c:1,d:1};
    json = eval('('+(JSON.stringify(json1)+JSON.stringify(json2)).replace(/}{/,',')+')');
    // json: {a:1,b:1,c:1,d:1}
     

    在javascript中,对象本身就是一种Map结构。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var map = {};
    map['key1'] = 1;
    map['key2@'] = 2;
     
    console.log(map['key1']);//结果是1.
    console.log(map['key2@']);//结果是2.
     
    //如果遍历map
    for(var prop in map){
        if(map.hasOwnProperty(prop)){
            console.log('key is ' + prop +' and value is' + map[prop]);
        }
    }

    动态拼接多个json对象为一个

    var json="";
    proSourceData.projectDesc=$("#proSourceDesc").val().trim();
    proSourceData.projectName=$("#proSourceName").val().trim();
    $("#usedTbody tr").each(function (index,item) {
    var proObj={};
    proObj[""+$(this).children().eq(0).text()+""]=$(this).children().eq(1).text();
    json+=JSON.stringify(proObj);
    json = json.replace(/}{/,',');
    });
    proSourceData.ruleList=eval("("+json+")");
    var strProSourceData=JSON.stringify(proSourceData);


    动态获取json中的value

     var ruleList=proResource.ruleList;
    var proStr="";
    for(var prop in ruleList){
    if(ruleList.hasOwnProperty(prop)){
    proStr+=ruleList[prop]+",";
    }
    ; }
  • 相关阅读:
    websocket简单理解
    对两个列表合成一个列表后进行排序
    爬取今日头条财经版块新闻
    Python的hasattr(),getattr(),setattr()
    Django基础
    pymysql模块的使用
    我一定要学好英语
    Django项目的创建
    MySQL数据库(安装+增删改查)
    jQuery
  • 原文地址:https://www.cnblogs.com/cxf520/p/5869304.html
Copyright © 2011-2022 走看看