JSON解析
JSON格式的生成
public static String Json(){
JSONObject obj = new JSONObject();//建立JSONObject
obj.put("name","张三");
JSONObject obj2 = new JSONObject();
obj2.put("name","李四");
JSONObject obj3 = new JSONObject();
obj3.put("name","王五");
JSONArray array = new JSONArray();//建立JSON数组
array.add(obj); //将object加入到数组中。
array.add(obj2);
array.add(obj3);
JSONObject clazz = new JSONObject();
clazz.put("clazzname","一年级一班");
clazz.put("num","3");
clazz.put("students",array);
System.out.println(clazz.toString());
return clazz.toString();
}
}
运行结果为:
{“clazzname”:”一年级一班”,”num”:”3”,”students”:[{“name”:”张三”},{“name”:”李四”},{“name”:”王五”}]}
JSON解析
String json=Json ();
if(json !=null){
JSONObject clazz = JSONObject.fromObject(json);//获得JSON解析的字符串
System.out.println(clazz.getString("num"));//对第一个JSONObject进行解析
JSONArray array = clazz.getJSONArray("students");//对数组进行解析。
for(int i =0;i<array.size();i++){
JSONObject obj = array.getJSONObject(i);//对数组内的每个JSONObject进行解析。
System.out.println(obj.getString("name"));
}
}
运行结果:
3
张三
李四
王五
Date与SimpleDateFormat
Date中类的方法大多已经过时,但它与SimpleDateFormat这个类结合可以使时间的显示格式化。
Date date = new Date();
//String s = "2015/07/24/11:41";
SimpleDateFormat format =new SimpleDateFormat("yyyy/MM/dd/kk:mm");
System.out.println(format.format(date));
运行结果如下:
2015/07/24/19:05;
运行格式的设置如下所标记的一样:
字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
通过这种格式产生的字符串可以被解析出来:
例如:
String s = "2015/07/24/11:41";
SimpleDateFormat format =new SimpleDateFormat("yyyy/MM/dd/kk:mm");
System.out.println(format.format(date));
try {
Date date = format.parse(s);//构造解析器
System.out.println(date.getHours());//对小时进行解析
} catch (ParseException e) {
e.printStackTrace();
}
运行结果为:11;
Calendar
一般用Calendar类进行对日期的获取。
Calendar calender = Calendar.getInstance();
System.out.println(calender.getTime());
System.out.println(calender.get(Calendar.MONTH));
System.out.println(calender.get(Calendar.DAY_OF_MONTH));
calender.add(Calendar.HOUR,55);
运行结果如下:
Fri Jul 24 19:18:26 CST 2015
6
24
Set,List,Map的说明
Collect有两个子接口:List和Set。
Collect中有许多的方法如:
add(),clear() ,isEmpty() ,iterator() ,remove(Object o) ,size() 等方法可以被List和Set的实现类所用。
List接口
List子内存中存储的数据是有序可重复的。
有两个实现子类:ArrayLis和LinedList
ArrayList在存储数据时是内存连续的空间,其特点是遍历快插入慢。
LineList存储数据时内存不连续,其特点是遍历慢但插入或删除快。
用代码实现一个用Sort方法的List集合的排序:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Tests {
public static void main(String[] args) {
List<Students> student = new ArrayList<Students>();
Students zhangsan = new Students("张三",20);
student.add(zhangsan);
Students lisi = new Students("李四",19);
student.add(lisi);
Students wangwu = new Students("王五",21);
student.add(wangwu);
Collections.sort(student, new Comparess());//在参数中student是要比较的集合的名字,new comparess是一个自我定义的继承自 Comparator接口的类。
for(Students s :student){
System.out.println(s.getName());
}
}
}
comparess接口类的代码
import java.util.Comparator;
public class Comparess implements Comparator<Students>{
//<Students>是告诉compare()是在比较哪一个变量。
public int compare(Students s1, Students s2) {
return s1.getAge()-s2.getAge();
}
}
Set接口
Set有两个实现类:HashSet和TreeSet
特点是存储的数据无需不可重复。
因为Set是无序的所以不能用for循环去遍历。必须用迭代器去遍历
iterator()方法是Collection接口的方法可以用Set实现类的对象去建立构造器。
HashSet<Integer> age = new HashSet<integer>();
Iterator<Integer> it = age.iterator();//创建迭代器
while(it.hasNext()){
System.out.println(it.next());
}
Map
Map是以键值对的形式存储的,通过键可以找的值是多少。
例如
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Tests {
public static void main(String[] args) {
HashMap<String , String> country = new HashMap<String , String>();
country.put("cn","中国");
country.put("JP","日本");
country.put("UK","英国");
country.put("US","美国");
//System.out.println(country.get("cn"));
HashMap<String , Students> clazz= new HashMap<String , Students>();
clazz.put("zahngsan", new Students("张三",20));
clazz.put("lisi", new Students("李四",25));
clazz.put("wangwu", new Students("王五",10));
Set<String> keys = clazz.keySet();//键的集合是Set类型的,所以用keySet()方法去获得Set的集合。
Iterator<String> it = keys.iterator();//通过迭代器去实现对键的遍历。
while(it.hasNext()){
String k = it.next();
System.out.println( k+"---->"+clazz.get(k).getName());
}
}
}
运行结果如下:
lisi———->李四
zahngsan———->张三
wangwu———->王五
HashMap允许null键和null值而HashMap却不允许。