标签:io os java ar for sp cti on c
前端根据后台返回json对象(Map)进行动态列展示。
javascript属性遍历和访问:
//var obj = {name:"h2do",age:0,sex:"男"}; function User(){ this.name = "h2do"; this.age = 0; this.sex = "男"; } var obj = new User(); /*遍历属性*/ //判断是否存在obj.hasOwnProperty("name"); for(var p in obj){ alert(p); } /*根据名称获取属性值*/ /*1、public属性*/ v = obj["name"];//同v = eval("obj."+"name"); alert(v); /*2、不存在属性*/ v = obj["address"]; alert(v);//undefined
对比java属性遍历和访问:
public static void main(String[] args) throws Exception { class User { public String name = "h2do"; public int age = 0; public char sex = ‘男‘; protected float money = 10*20*1000; } User obj = new User(); /*遍历属性*/ for(Field f : User.class.getFields()){ System.out.println(f.getName()); } /*根据名称获取属性值*/ /*1、public属性*/ System.out.println(User.class.getField("name").get(obj)); System.out.println(User.class.getField("age").getInt(obj)); System.out.println(User.class.getField("sex").getChar(obj)); /*2、不存在属性*/ try{ System.out.println(User.class.getField("address").get(obj)); }catch(NoSuchFieldException e){ System.out.println("属性不存在。"); } /*3、非public属性*/ try{ System.out.println(User.class.getField("money").get(obj)); }catch(NoSuchFieldException e){ System.out.println("属性不存在。"); } }
标签:io os java ar for sp cti on c
原文地址:http://my.oschina.net/h2do/blog/314870