JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
JSON数据作为目前网络中主流的数据传输格式之一,应用十分广泛。
对象:JSONObject对象表示为“{}”扩起来的内容,数据结构为 {key:value,key:value,…}的键值对的结构。
例如:{name:zhangsan,age:20}
数组:JSONArray数组表示为中括号“[]”扩起来的内容,数
据结构为 [value,value,value,…]的单值的结构。
例如:[“北京”,”上海”,”广州”]
创建json字符串
需要json-lib.jar包
1、{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”}
public class CreateJsonDemo1 {
public static void main(String[] args) {
Student student = new Student("张三",20,12,"广州");
//创建JSONObject对象
JSONObject obj = new JSONObject(student);
String str = obj.toString();
System.out.println(str);
}
}
2、{“student”:{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”}}
public static String createJSON2(){
Student student = new Student("张三",20,12,"广州");
JSONObject obj1 = new JSONObject(student);
JSONObject obj2 = new JSONObject();
try {
obj2.put("student", obj1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj2.toString();
}
3、[{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”},{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”},{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”}]
public static String createJSON3(){
Student student1 = new Student("张三",20,12,"广州");
Student student2= new Student("张三",20,12,"广州");
Student student3= new Student("张三",20,12,"广州");
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
JSONArray array = new JSONArray(list);
return array.toString();
}
解析json字符串
1、{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”}
public class JSONDemo1 {
public static void main(String[] args) throws JSONException {
String str = CreateJSONUtils.createJSON1();
System.out.println(str);
JSONObject obj = new JSONObject(str);
Student student = new Student();
String name = obj.getString("name");
student.setName(name);
int id = obj.getInt("id");
student.setId(id);
String address = obj.getString("address");
student.setAddress(address);
int age = obj.getInt("age");
student.setAge(age);
System.out.println(student);
}
}
2{“student”:{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”}}
public class JSONDemo2 {
public static void main(String[] args) throws JSONException {
String str = CreateJSONUtils.createJSON2();
System.out.println(str);
JSONObject obj1 = new JSONObject(str);
JSONObject obj2 = obj1.getJSONObject("student");
Student student = new Student();
student.setAddress(obj2.getString("address"));
student.setAge(obj2.getInt("age"));
student.setId(obj2.getInt("id"));
student.setName(obj2.getString("name"));
System.out.println(student);
}
}
3、[{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”},
{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”},
{“id”:12,”address”:”广州”,”age”:20,”name”:”张三”}]
public class JSONDemo3 {
public static void main(String[] args) throws JSONException {
String str = CreateJSONUtils.createJSON3();
System.out.println(str);
List<Student> list = new ArrayList<Student>();
JSONArray array =new JSONArray(str);
for(int i=0;i<array.length();i++){
JSONObject obj = array.getJSONObject(i);
Student student = new Student();
student.setAddress(obj.getString("address"));
student.setAge(obj.getInt("age"));
student.setId(obj.getInt("id"));
student.setName(obj.getString("name"));
list.add(student);
}
for(Student s:list){
System.out.println(s);
}
}
}
FastJson
FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分。
特点:
速度最快:测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson。
功能强大:完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自定义类型。
无依赖:能够直接运行在Java SE 5.0以上版本
支持Android
开源 (Apache 2.0)
使用:(需要fastjson.jar)
//一个对象。{"address":"广州","age":20,"id":1503,"name":"张三"}
public class FastJsonDemo1 {
public static void main(String[] args) {
Student student = new Student("张三", 20, 1503, "广州");
// 1:生成json字符串
String json = JSON.toJSON(student).toString();
System.out.println(json);
// 2;解析成对象。
Student stu = JSON.parseObject(json, Student.class);// 反射
System.out.println(stu);
}
}
//[{"address":"广州","age":20,"id":1503,"name":"张三"},{"address":"广州","age":20,"id":1503,"name":"张三"},{"address":"广州","age":20,"id":1503,"name":"张三"}]
public class FastJsonDemo2 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("张三", 20, 1503, "广州"));
list.add(new Student("张三", 20, 1503, "广州"));
list.add(new Student("张三", 20, 1503, "广州"));
String json = JSON.toJSON(list).toString();
System.out.println(json);
//解析
List<Student> array = JSON.parseArray(json, Student.class);
for(Student s:array){
System.out.println(s);
}
}
}
通常和Json结合使用
//json跟fastjson结合使用。
public class FastjsonDemo3 {
public static void main(String[] args) throws Exception {
String path = "http://litchiapi.jstv.com/api/GetFeeds?column=7&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
byte[] b = HttpUtils.loadData(path);
List<News> list = new ArrayList<News>();
String json = new String(b,0,b.length,"utf-8");
System.out.println(json);
JSONObject obj1 = new JSONObject(json);
JSONArray array = obj1.getJSONObject("paramz").getJSONArray("feeds");
for(int i=0;i<array.length();i++){
JSONObject obj2 = array.getJSONObject(i).getJSONObject("data");
News news = JSON.parseObject(obj2.toString(), News.class);
list.add(news);
}
for(News n:list){
System.out.println(n);
}
}
}
Gson(需要gson.jar)
Gson这个Java类库可以把Java对象转换成JSON,也可以把JSON字符串转换成一个相等的Java对象。
Gson支持任意复杂Java对象包括没有源代码的对象。
public class GSONDemo1 {
public static void main(String[] args) {
Student student = new Student("张三", 20, 1503, "广州");
// 创建GSON对象
Gson gson = new Gson();
String str = gson.toJson(student);
System.out.println(str);
//解析成对象
Student stu = gson.fromJson(str, Student.class);
System.out.println(stu);
}
}