java面试题6
12.20
一、json 的数据解析:
1.将对象(集合)转化为json字符串:
对象:
User u1 = new User(1,"tom","123456");
String js = JsonObject.ToJsonString(u1);
sout(js);
json集合:
List<User> list = new List<User>();
list.add(new User(2,"tony","123456"));
list.add(new User(2,"tony","123456"));
list.add(new User(2,"tony","123456"));
String js1 = JsonArray.ToJsonString(list);
sout(js1);
2.从json字符串到对象(集合):
User us = JsonObject.parseObject(u1,User.class);
sout(us);
得到对象的集合、解析、遍历输出:
User a = JsonObject.parseObject(js1,User.class);
for(User uu:a){
sout(uu);
}
二、 增强型循环:
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}