JsonUtil工具类


package com.example.jsonutil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

public static <T> List<T> parseJson(Class<T> clzz, String jsonObjArray) {

List<T> modelVOList = new ArrayList<T>();
try {

JSONArray jsonArray = new JSONArray(jsonObjArray);

if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
T object = null;
try {
object = clzz.newInstance();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
}
JSONObject obj = jsonArray.getJSONObject(i);

Field[] fields = object.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
String fieldName = fields[j].getName();
String typeName = fields[j].getType().getCanonicalName().substring(fields[j].getType().getCanonicalName().lastIndexOf(".") + 1);
Method jsonObjGetMethod = null;
Method vehicleVOSetMethod = null;
try {
String jsonGetMethodName = "get" + typeName;
if ("Integer".equals(typeName) || "BigDecimal".equals(typeName)) {
jsonGetMethodName = "getInt";
}
String firstChar = String.valueOf(fieldName.toUpperCase(Locale.US).charAt(0));
String vehicleVOSetMethodName = "set" + firstChar + fieldName.substring(1);
jsonObjGetMethod = obj.getClass().getDeclaredMethod(jsonGetMethodName, new Class[]{String.class});
vehicleVOSetMethod = object.getClass().getDeclaredMethod(vehicleVOSetMethodName, new Class[]{fields[j].getType()});

Object valueObject = jsonObjGetMethod.invoke(obj, new Object[]{fieldName});
if (valueObject instanceof String) {
vehicleVOSetMethod.invoke(object, new String[]{(String)valueObject});
} else if (valueObject instanceof Long) {
vehicleVOSetMethod.invoke(object, new Long[]{(Long)valueObject});
} else if (valueObject instanceof Double) {
vehicleVOSetMethod.invoke(object, new Double[]{(Double)valueObject});
} else if (valueObject instanceof Integer) {
vehicleVOSetMethod.invoke(object, new Integer[]{(Integer)valueObject});
} else if (valueObject instanceof BigDecimal) {
vehicleVOSetMethod.invoke(object, new BigDecimal[]{(BigDecimal)valueObject});
}

} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
modelVOList.add(object);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return modelVOList;
}
}


使用范例:

People people = new People();
people.setId("111");
people.setAge(12);
people.setName("zhelong");
people.setWeight(111.2);

List<People> peopleList = new ArrayList<People>();
peopleList.add(people);
peopleList.add(people);
peopleList.add(people);


Gson gson = new Gson();

String jsonString = gson.toJson(peopleList);

List<People> peoples = JsonUtil.parseJson(People.class, jsonString);
for (People p : peoples) {
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getWeight());
System.out.println(p.getAge());
System.out.println("------------------------------");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值