public class JsonToJavaUtil {
/**
* 将json转成成javaBean对象
*
* @param
* 返回类型
* @param json
* 字符串
* @param clazz
* 需要转换成的类
* @return
*/
public static List jsonToJavaBean(String json, Type type) {
List list = new ArrayList();
try {
Gson gson = new Gson();
list = gson.fromJson(json, type);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
-------------------
测试代码:
String str = "[{code:'0',name:'we',hsCode:'rwer',jtCode:'1',invoiceno:'r12',weight:12,capacity:45},{code:'0',name:'fgh',hsCode:'fgh',jtCode:'1',invoiceno:'dfg',weight:45,capacity:45}]";
Gson gson = new Gson();
List list = JsonToJavaUtil.jsonToJavaBean(str, new TypeToken>() {}.getType());
for (int i = 0; i < list.size(); i++) {
System.out.println("==================="+list.get(i).getName());
}
测试结果:
===================we
===================fgh