JSONObject
JSONObject jsonObject = new JSONObject(strJson);
JSONArray jsonArray = jsonObject.getJSONArray("data");
{} Jsonstr bean bean1....
[]
大括号{}
Gson gson = new Gson();
bean b1 = gson.fromJson(Jsonstr,bean.class)
中括号[]:
Gson
gson = new Gson();
List<bean> list = gson.fromJson(jsonString, new TypeToken<List<bean.class>>() {}.getType());
gson = new Gson();
List<bean> list = gson.fromJson(jsonString, new TypeToken<List<bean.class>>() {}.getType());
Gson:
使用泛型获取javaBean(核心函数)
(1),对象类型数据
public static <T> T getPerson(String jsonString, Class<T> cls) {
T
t = null;
try
{
Gson
gson = new Gson();
t
= gson.fromJson(jsonString, cls);
}
catch (Exception e) {
//
TODO: handle exception
}
return
t;
}
(2),集合类型数据
public static <T> List<T> getPersons(String jsonString, Class<T> cls) {
List<T>
list = new ArrayList<T>();
try
{
Gson
gson = new Gson();
list
= gson.fromJson(jsonString, new TypeToken<List<cls>>() {
}.getType());
}
catch (Exception e) {
}
return
list;
}
T
t = null;
try
{
Gson
gson = new Gson();
t
= gson.fromJson(jsonString, cls);
}
catch (Exception e) {
//
TODO: handle exception
}
return
t;
}
(2),集合类型数据
public static <T> List<T> getPersons(String jsonString, Class<T> cls) {
List<T>
list = new ArrayList<T>();
try
{
Gson
gson = new Gson();
list
= gson.fromJson(jsonString, new TypeToken<List<cls>>() {
}.getType());
}
catch (Exception e) {
}
return
list;
}
(3),集合中含map类型数据
public static List<Map<String, Object>> listKeyMaps(String jsonString) {
List<Map<String,
Object>> list = new ArrayList<Map<String, Object>>();
try
{
Gson
gson = new Gson();
list
= gson.fromJson(jsonString,
new
TypeToken<List<Map<String, Object>>>() {
}.getType());
}
catch (Exception e) {
//
TODO: handle exception
}
return
list;
}
public static List<Map<String, Object>> listKeyMaps(String jsonString) {
List<Map<String,
Object>> list = new ArrayList<Map<String, Object>>();
try
{
Gson
gson = new Gson();
list
= gson.fromJson(jsonString,
new
TypeToken<List<Map<String, Object>>>() {
}.getType());
}
catch (Exception e) {
//
TODO: handle exception
}
return
list;
}
eg:
public static Mode getJson2(String json){
Gson gson = new Gson();
Mode mode = gson.fromJson(json, Mode.class);
return mode;
}
FastJson:
(1),集合类型数据
List<T> datas = JSON.parseArray(str,T.class):解析一个数组 {jsonArray} [,,,,,,]
(2),对象类型数据
T t = JSON.parseObject(str,T.class):解析一个对象 {jsonObject} {}
FastJson对象的知识点:
fastjson 是一个性能极好的用 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。
主要特点:
快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)
强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
零依赖(没有依赖其它任何类库除了JDK)
一 、生成Json:
JavaBean、List<JavaBean>、List<String>、List<Map<String,Object>>
- String jsonString = JSON.toJSONString(obj);
二、解析Json:
(1)JavaBean
Class class= JSON.parseObject(jsonString, Class.class);
(2)List<JavaBean
List<Class> class=JSON.parseArray((jsonString, Class.class);
(3)List<String>
List<String> listString = JSON.parseArray(jsonString, String.class);
(4)List<Map<String,Object>>
List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});