一、json对应的基础
1、JSON 数据的书写格式是:名称/值对 "firstName" : "John"
2、JSON 值
JSON 值可以是:1、数字(整数或浮点数)2、字符串(在双引号中)3、逻辑值(true 或 false)4、数组(在方括号中)5、对象(在花括号中)6、null
3、JSON 对象
JSON 对象在花括号中书写,对象可以包含多个名称/值对:{ "firstName":"John" , "lastName":"Doe" }
4、JSON 数组
JSON 数组在方括号中书写:数组可包含多个对象
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
5、JSON 文件
JSON 文件的文件类型是 ".json"
JSON 文本的 MIME 类型是 "application/json"
二、通过json语句,分析语句对应的实体类,
这里次啊用:hijson工具以及火狐:restclient
1、语句:http://zhbj.qianlong.com/static/api/news/categories.json
2、返回json:
{"retcode":200,"data":[{"id":10000,"title":"新闻","type":1,"children":[{"id":10007,"title":"北京","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10007/list_1.json"},{"id":10006,"title":"中国","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10006/list_1.json"},{"id":10008,"title":"国际","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10008/list_1.json"},{"id":10014,"title":"文娱","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10014/list_1.json"},{"id":10010,"title":"体育","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10010/list_1.json"},{"id":10091,"title":"生活","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10091/list_1.json"},{"id":10012,"title":"旅游","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10012/list_1.json"},{"id":10095,"title":"科技","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10095/list_1.json"},{"id":10009,"title":"军事","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10009/list_1.json"},{"id":10011,"title":"财经","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10011/list_1.json"},{"id":10093,"title":"时尚","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10093/list_1.json"},{"id":10192,"title":"倍儿逗","type":1,"url":"http://zhbj.qianlong.com/static/api/news/10192/list_1.json"}]},{"id":10002,"title":"专题","type":10,"url":"http://zhbj.qianlong.com/static/api/news/10002/list_1.json","url1":"http://zhbj.qianlong.com/static/api/news/10002/list1_1.json"},{"id":10003,"title":"组图","type":2,"url":"http://zhbj.qianlong.com/static/api/news/10003/list_1.json"},{"id":10004,"title":"互动","type":3,"excurl":"http://zhbj.qianlong.com/static/api/news/comment/exc_1.json","dayurl":"http://zhbj.qianlong.com/static/api/news/comment/day_1.json","weekurl":"http://zhbj.qianlong.com/static/api/news/comment/week_1.json"},{"id":10005,"title":"投票","type":4,"url":"http://zhbj.qianlong.com/static/api/news/vote/vote_1.json"}],"extend":[10007,10006,10008,10014,10091,10010,10192,10009,10095,10093]}
3、得到bean
import java.util.List;
public class NewsCenterCategory {
public List<CenterCategory> data;
public List<Integer> extend;
public int retcode;
public static class CenterCategory{
public List<CenterCategoryItem> children;
public int id;
public String title;
public int type;
public String url;
public String url1;
public String dayurl;
public String excurl;
public String weekurl;
}
public static class CenterCategoryItem{
public String id;
public String title;
public String type;
public String url;
}
}
三、由上面的数据结构,完成最后的解析工作:
1、这里利用google的GSON包来解析,案例如下:
package com.json.gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.json.bean.Person;
/*
* 这里将json的转化和解析都放在一起了,大家可以根据实际需要来转化json字符串和解析json字符串
*/
public class GsonClass {
public static void main(String[] args) {
method1();
method2();
method3();
method4();
}
static void method1(){
System.out.println("javabean转化示例开始----------");
Person person = new Person("1","gson",1);
Gson gson = new Gson();
//这里将javabean转化成json字符串
String jsonString = gson.toJson(person);
System.out.println(jsonString);
//这里将json字符串转化成javabean对象,
person = gson.fromJson(jsonString,Person.class);
System.out.println(person.toString());
System.out.println("javabean转化示例结束----------");
}
static void method2(){
System.out.println("List<javabean>转化示例开始----------");
Person person1 = new Person("1","gson1",1);
Person person2 = new Person("2","gson2",2);
List<Person> persons = new ArrayList<Person>();
persons.add(person1);
persons.add(person2);
Gson gson = new Gson();
String jsonString = gson.toJson(persons);
System.out.println("json字符串:"+jsonString);
//解析json字符串
List<Person> persons2 = gson.fromJson(jsonString, new TypeToken<List<Person>>(){}.getType());
//输出解析后的person对象,也可以通过调试模式查看persons2的结构
System.out.println("person1对象:"+persons2.get(0).toString());
System.out.println("person2对象:"+persons2.get(1).toString());
System.out.println("List<javabean>转化示例结束----------");
}
static void method3(){
System.out.println("List<String>转化示例开始----------");
List<String> list = new ArrayList<String>();
list.add("gson1");
list.add("gson2");
list.add("gson3");
Gson gson = new Gson();
String jsonString = gson.toJson(list);
System.out.println("json字符串:"+jsonString);
//解析json字符串
List<String> list2 = gson.fromJson(jsonString, new TypeToken<List<String>>(){}.getType());
System.out.println(list2.get(0)+"::"+list2.get(1)+"::"+list2.get(2));
System.out.println("List<String>转化示例结束----------");
}
static void method4(){
System.out.println(" List<Map<String,Object>>转化示例开始----------");
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1", "value1");
map.put("key2", "value2");
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("key1", 1);
map2.put("key2", 2);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
list.add(map);
list.add(map2);
Gson gson = new Gson();
String jsonString = gson.toJson(list);
System.out.println("json字符串:"+jsonString);
//解析json字符串
List<Map<String,Object>> list2 = gson.fromJson(jsonString, new TypeToken<List<Map<String,Object>>>(){}.getType());
System.out.println("map的key1值"+list2.get(0).get("key1"));
System.out.println("map的key2值"+list2.get(0).get("key2"));
System.out.println("ma2p的key1值"+list2.get(1).get("key1"));
System.out.println("map2的key2值"+list2.get(1).get("key2"));
System.out.println(" List<Map<String,Object>>转化示例结束----------");
}
}