代码下载:点击打开链接
Json解析工具类完善一下,使用GSON+JsonPath+泛型来提高自己写JSON解析的效率 如下关于JsonPath
http://www.7mdm.com/1374.html
https://github.com/jayway/JsonPath
为json解析轻松加上Integer Double String List
比如这段json
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"isbn": "0-553-21311-3"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Java获取所有作者
List
输出=====> [Nigel Rees, Evelyn Waugh]
Double d = JsonPathGeneric.getGeneric(json,"$.store.bicycle.price",Double.class);
输出=====>19.95public class BeanBook implements Serializable {
public String author;
public Double price;
public String category;
public String title;
@Override
public String toString() {
return "Book [author=" + author + ", price=" + price + ", category="
+ category + ", title=" + title + "]";
}
}
BeanBook bean=JsonPathGeneric.getGenericObject(json,"$.store.book[0]", BeanBook.class);
输出=====>Book [author=Evelyn Waugh, price=12.99, category=fiction, title=Sword of Honour]
除了静态的方法还有指定继承关系的json泛型强转
来一个开发的例子如下json1
{
"status": "success",
"result": {
"user_id": 4,
"nickname": "",
"xiaoqu_id": 1
}
}
json2
{
"status": "success",
"result": [
{
"user_id": 4,
"nickname": "",
"xiaoqu_id": 1
},
{
"user_id": 5,
"nickname": "",
"xiaoqu_id": 3
}
]
}
public class BeanUser {
public String xiaoqu_id ;
public String user_id ;
public String nickname ;
}
System.out.println(JsonPathGeneric.getGeneric(json1, "$.status", String.class));
{
System.out.println(json1);
String id = new Generic(json1, "$.result") {
}.getJsonBean().user_id;
System.out.println("user_id=" + id);
}
{
System.out.println(json2);
List list = new Generic>(json2, "$.result") {
}.getJsonBean();
System.out.println("user_id=" + list.get(1).user_id);
}
打印结果
success
user_id=4
user_id=5
以下是工具类代码:
package org.utils.json;
import java.lang.reflect.Type;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jayway.jsonpath.JsonPath;
public class JsonPathGeneric {
@SuppressWarnings("unchecked")
public static T getGeneric(String json, String jsonPath, Class clazz) {
return new JsonPathGeneric(clazz, json, jsonPath).getObject();
}
@SuppressWarnings("unchecked")
public static T getGenericObject(String json, String jsonPath,
Class clazz) {
String jsonResult =null;
if(jsonPath==null||"".equals(jsonPath))
{
jsonResult=json;
}else
{
jsonResult = JsonPath.read(json, jsonPath).toString();
}
if (String.class.equals(clazz)) {
return (T) jsonResult;
}
return (T) new Gson().fromJson(jsonResult, clazz);
}
@SuppressWarnings("unchecked")
public static List getGenericList(String json, String jsonPath,
Class clazz) {
String jsonResult =null;
if(jsonPath==null||"".equals(jsonPath))
{
jsonResult=json;
}else
{
jsonResult = JsonPath.read(json, jsonPath).toString();
}
Type listtype = new TypeToken>() {
}.getType();
return (List) new Gson().fromJson(jsonResult, listtype);
}
public JsonPathGeneric() {
}
private String json;
private String jsonPath;
private String jsonResult;
private Type type;
private T object;
/**继承关系时注意父类传值*/
public JsonPathGeneric(Object obj, String json, String jsonPath) {
if (obj instanceof Type) {
parse((Type) obj, json, jsonPath);
} else {
parse(ReflectionUtil.getParameterizedTypes(obj)[0], json, jsonPath);
}
}
public JsonPathGeneric(Type type, String json, String jsonPath) {
parse(type, json, jsonPath);
}
public JsonPathGeneric(String json, String jsonPath) {
Type[] parameterizedTypes = ReflectionUtil.getParameterizedTypes(this);
type = parameterizedTypes[0];
parse(type, json, jsonPath);
}
@SuppressWarnings("unchecked")
public void parse(Type type, String json, String jsonPath) {
this.json = json;
this.jsonPath = jsonPath;
this.type = type;
if(jsonPath==null||"".equals(jsonPath))
{
jsonResult=json;
}else{
this.jsonResult = JsonPath.read(json, jsonPath).toString();
}
Class clazz = null;
try {
clazz = (Class) ReflectionUtil.getClass(type);
} catch (ClassNotFoundException e) {
}
if (clazz != null) {
if (String.class.equals(clazz)) {
object = (T) jsonResult;
} else {
object = new Gson().fromJson(jsonResult, clazz);
}
} else {
object = new Gson().fromJson(jsonResult, type);
}
// System.out.println("-------------------------------");
// System.err.println(jsonResult);
// System.err.println(object.getClass().getSimpleName());
// System.err.println(object);
// System.out.println("-------------------------------");
}
public String getJson() {
return json;
}
public JsonPathGeneric setJson(String json) {
this.json = json;
return this;
}
public String getJsonPath() {
return jsonPath;
}
public JsonPathGeneric setJsonPath(String jsonPath) {
this.jsonPath = jsonPath;
return this;
}
public String getJsonResult() {
return jsonResult;
}
public Type getType() {
return type;
}
public JsonPathGeneric setType(Type type) {
this.type = type;
return this;
}
public T getObject() {
return object;
}
}
public class Generic {
public Generic(String json, String jsonPath) {
// TODO Auto-generated constructor stub
/**继承关系时注意父类传值this*/
jsonBean = new JsonPathGeneric(this, json, jsonPath).getObject();
}
public T jsonBean;
public T getJsonBean() {
return jsonBean;
}
public void setJsonBean(T jsonBean) {
this.jsonBean = jsonBean;
}
}
代码下载:点击打开链接