js对象输出

例子一

<script>
var obj  = {
attribute:1,
method:function() {
alert("我是函数");
}
}
for (var i in obj){
alert(i);            // 输出属性名:  attribute,method
alert(obj[i])        // 输出属性的值:1和函数的内容
alert(obj["method"]);// 输出指定的值:如果只知道属性的几个字母,那么可以结合正则表达式输出这个属性
}
</script>

 

 

例子二

<script>
//定义json对象
var person= {
name: {a:'zhangsan'},
pass: '123',
fn: function(){
alert(this.name+"的密码="+this.pass);
}
}
//遍历person属性包括方法,如果不想显示出方法,可用typeof(person[item])== "function"来判断
for(var item in person){
alert("person中"+item+"的值="+person[item]);
}
</script>

例子三

<script type='text/javascript'>
/*var txt = '{'total':'4','+''employees':[' +
'{'firstName':'Bill','lastName':'Gates' },' +
'{'firstName':'George','lastName':'Bush' },' +
'{'firstName':'Thomas','lastName':'Carter' }]}';*/

var txt = {'total':'4','data':[{'id':'11','class_name':'\u8ba109A-2','attach_major':'\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f','attach_major_id':'4','his_year':'2009','is_show':'1'},{'id':'12','class_name':'\u8ba109A-3','attach_major':'\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f','attach_major_id':'4','his_year':'2009','is_show':'1'},{'id':'13','class_name':'\u8ba109A-4','attach_major':'\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f','attach_major_id':'4','his_year':'2009','is_show':'1'}]}

//var obj = eval ('(' + txt + ')');
//alert(txt.total);
alert(txt.data[1].class_name);

//document.getElementById('fname').innerHTML=obj.employees[2].firstName
//document.getElementById('lname').innerHTML=obj.employees[1].lastName
</script>

posted @ 2013-03-07 20:23  joy696163  阅读(12171)  评论(0编辑  收藏  举报