JSON的应用
DATA类
CollectionAndMap
泛型
JSON的构造
1.JSON的构造在java里面需要导入有关json的包
2.JSON的构造比较简单,只需要构建一个JSONObject就相当于构建完成了利用put方法将内容写进去;利用add方法向JSON数组添加内容
public static String creatJSON(){
JSONObject obj=new JSONObject();
obj.put("name", "张三");
JSONObject obj2=new JSONObject();
obj2.put("name", "李四");
JSONObject obj3=new JSONObject();
obj3.put("name", "王五");
JSONArray array=new JSONArray();
array.add(obj);
array.add(obj2);
array.add(obj3);
JSONObject clazz=new JSONObject();
clazz.put("clazzname", "一年级一班");
clazz.put("num", "3");
clazz.put("student", array);//将数组array的内容放入到student中
System.out.println(clazz.toString());//打印clazz中的字符内容
return clazz.toString();
public static String creatJSONObject(){
Student zhangsan=new Student(20,"张三");//一个Student类,数据的输入
Student lisi=new Student(20,"李四");
Student wangwu=new Student(20,"王五");
Student zhaoliu=new Student(20,"赵六");
JSONObject obj=new JSONObject();//定义一个JSONObject
JSONArray array=new JSONArray();//定义一个JSONArray
array.add(zhangsan);//将Student类对象添加到JSONArray的数组里面
array.add(lisi);
array.add(wangwu);
array.add(zhaoliu);
obj.put("clazz", array);//将数组放到clazz里面
System.out.println(obj.toString());
return obj.toString();
}
3.输出JSON中内容的一般方法
public static void main(String[] args) {
// File file=new File("e:\\22.txt");
String json01=creatJSONObject();
String json=creatJSON();//构建一个JSON文档
if (null!=json) {
JSONObject clazz=JSONObject.fromObject(json);//定义一个名为clazz的JSONObject其数据来源是json文件
System.out.println(clazz.getString("clazzname"));//打印clazzname中的内容
JSONArray array=clazz.getJSONArray("student");//打印数组student中的内容
for (int i = 0; i < array.size(); i++) {
JSONObject obj=array.getJSONObject(i);//将数组里面的内容重新导入到一个JSONObject中
System.out.println(obj.getString("name"));
}
}
}
DATA类
1.data类大部分已经过时了,现在常用的是Calendar类
2.由于Calendar类属于抽象类,不能直接构建对象,因此可以利用getInstance方法来得到一个对象
3.Calendar类的一般用法如下
public static void main(String[] args) {
Date date=new Date();
System.out.println(date.getTime());//返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数
//Calendar是抽象类 getInstance使用默认时区和语言环境获得一个日历。
Calendar calendar=Calendar.getInstance();
System.out.println(calendar.get(calendar.HOUR_OF_DAY));
System.out.println(calendar.get(calendar.MONTH));
System.out.println(calendar.get(calendar.YEAR));
System.out.println(calendar.get(calendar.DAY_OF_MONTH));
calendar.add(calendar.DAY_OF_MONTH, 50);
System.out.println(calendar.get(calendar.DAY_OF_MONTH));
}
format类的简单应用
1.由于Format类属于抽象类,所以可以利用public子类构建对象
2.构建的对象是以自己定义的格式对时间进行规范、
3.parse解析的内容必须是按照自己规定好的格式写的。否则系统会报错
Date date=new Date();
// 定义format的格式为"kk:mm:ss MM/dd/yyyy"
SimpleDateFormat format=new SimpleDateFormat("kk:mm:ss MM/dd/yyyy");
System.out.println(format.format(date));// 将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer
String s="11:39:12 07/24/2015";
// String s="07/24/2015 11:39:12 ";//系统会报错,错误提示Unknown source
try {
Date date1=format.parse(s);//解析字符串s中的格式依据是自己定义的格式"kk:mm:ss MM/dd/yyyy"
System.out.println(date1.getDate());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CollectionAndMap的关系图
List的简单应用
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();//构建一个list的的数组类型为Student
Student zhangsan = new Student(20, "张三");
Student lisi = new Student(25, "李四");
Student wangwu = new Student(28, "王五");
Student zhaoliu = new Student(30, "赵六");
Student maqi = new Student(10, "马齐");
students.add(zhangsan);
students.add(lisi);
students.add(wangwu);
students.add(zhaoliu);
students.add(maqi);
// 这两个for循环的功能是一样的
// for (int i = 0; i < students.size(); i++) {
// Student s=students.get(i);//返回列表中指定的值
// System.out.println("年龄"+s.getAge()+"姓名"+s.getName());
// }
for (Student student : students) {
System.out.println("年龄" + student.getAge() + "姓名" + student.getName());
}
// 按照年龄的大小进行排序
// 比冒泡排序法的作用大多了
Collections.sort(students, new MyCompare());//根据指定比较器产生的顺序对指定列表进行排序
for (Student student : students) {
System.out.println("年龄" + student.getAge() + "姓名" + student.getName());
}
}
Set的简单应用
public static void main(String[] args) {
HashSet<Integer> set=new HashSet<Integer>();
Random random=new Random();
while(set.size()<10){
int i=random.nextInt(90)+10;
set.add(i);//由于是hashset编码,重复的部分不会被添加进去。
}
// 由于HashSet中没有get函数因此只能用迭代的方式来打印里面的内容
Iterator it=set.iterator();//返回对此 set 中元素进行迭代的迭代器
while(it.hasNext()){//hasnext的作用是 如果仍有元素可以迭代,则返回 true。
System.out.println(it.next());
}
}
Map的简单应用
HashMap<String, String> counties=new HashMap<String, String>();
counties.put("CN", "中国");
counties.put("USA", "美国");
counties.put("JP", "日本");
counties.put("UK", "英国");
System.out.println(counties.get("USA"));//由于map是
HashMap<String, Student> clazz=new HashMap<String, Student>();
clazz.put("zhangsan", new Student(22,"张三") );
clazz.put("lisi", new Student(22,"李四") );
clazz.put("wangwu", new Student(22,"王五") );
clazz.put("zhaoliu", new Student(22,"赵六") );
Set<String> key=clazz.keySet();//返回此映射中所包含的键的 Set 视图。
Iterator<String> it=key.iterator();//返回在此 set 中的元素上进行迭代的迭代器
while(it.hasNext()){
String keys=it.next();//it.next只能用一次。因为运行一次就返回下一个元素
System.out.println(keys+"-----"+clazz.get(keys).getName());
}
}
泛型集合解决的主要问题
1.把任何类型对象通过add(Object obj)放入List中,认为只是Object类型
2.通过get(int index)取出List中元素时必须进行强制类型转换,繁琐而且容易出现异常
3.使用Map的put(Object key,Object value)和get(Object key)存取对象时存在同样的问题
4.使用Iterator的next()方法获取元素时存在同样的问题
public static void main(String[] args) {
// 声明一个Student类中含有泛类《Cat》
Student<Cat> zhangsan=new Student<Cat>();
Cat tom=new Cat();
zhangsan.setPet(tom);
zhangsan.getPet();
Student<Dog> lisi=new Student<>();
lisi.setPet(new Dog());
Student<Pet> wangwu=new Student<>();
wangwu.setPet(new Pet());
public class Student<T> {
private T pet;
public T getPet() {
return pet;
}
public void setPet(T pet) {
this.pet = pet;
}