1. 程式人生 > >後臺map 轉為json 傳到前臺 構建js物件陣列

後臺map 轉為json 傳到前臺 構建js物件陣列

後臺java程式碼

Map listMap = new HashMap();
listMap.put("10", "熱賣商品");
listMap.put("11", "限時搶購");
JsonConfig config = new JsonConfig();
String rs = JSONObject.fromObject(listMap, config).toString();
out.print(rs);

{"10":"熱賣商品","11":"限時搶購"}       ------ >      var data = [{ id: 10, text: '熱賣商品' }, { id:11, text: '限時搶購'}];


前臺js:

var typeData = [];

...ajax..................

success: function (text) {

    	    	var o = mini.decode(text);
    	        for(i in o)
    	        {
    	        	var obj = {};
    	        	obj.id = i;
    	        	obj.text = o[i];
    	        	typeData.push(obj);
    	        	alert(obj.id);
    	        	alert(obj.text);
    	        	alert(typeData);
    	        }
    	        
    	    },
........................