您的位置:首页 > Web前端 > JavaScript

工具类--JsonUtils 数据解析

2016-08-22 14:09 411 查看
/***
* json工具解析类
*/
public class JsonUtils {

private static Gson gson = new Gson();

public static <T> T toBean(Class<T> type, String s) {
if (s == null) {
return null;
}
return gson.fromJson(s, type);
}

public static <T> T toBean(Type type, String s) {
if (s == null) {
return null;
}
return gson.fromJson(s, type);
}

public static <T> List<T> getList(Class<T[]> type, String s) {
if (s == null) return null;
List<T> results = new ArrayList<T>();
try {
T[] _next = toBean(type, s);
if (_next != null)
Collections.addAll(results, _next);
} catch (Exception e) {
return null;
}
return results;
}

public static <T> T toBean(Class<T> type, Reader reader) throws IOException {

TypeAdapter<T> adapter = gson.getAdapter(type);
JsonReader jsonReader = gson.newJsonReader(reader);
try {
return adapter.read(jsonReader);
} finally {
jsonReader.close();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: