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]+",";
}
; }