| | |
| | | import java.lang.reflect.Type;
|
| | | import java.math.BigDecimal;
|
| | | import java.text.DateFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | |
|
| | | import com.google.gson.ExclusionStrategy;
|
| | | import com.fasterxml.jackson.core.JsonProcessingException;
|
| | | import com.fasterxml.jackson.databind.JavaType;
|
| | | import com.fasterxml.jackson.databind.ObjectMapper;
|
| | | import com.google.gson.Gson;
|
| | | import com.google.gson.GsonBuilder;
|
| | | import com.google.gson.JsonElement;
|
| | |
| | | import com.google.gson.stream.JsonReader;
|
| | | import com.google.gson.stream.JsonWriter;
|
| | |
|
| | | import net.sf.json.JSONArray;
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class JsonUtil {
|
| | |
|
| | | static Gson baseGson = new Gson();
|
| | |
|
| | | public static String loadTrueResult(Object result) {
|
| | | JSONObject object = new JSONObject();
|
| | | object.put("code", 0);
|
| | |
| | | return map;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 将对象转换成json字符串。
|
| | | * <p>
|
| | | * Title: pojoToJson
|
| | | * </p>
|
| | | * <p>
|
| | | * Description:
|
| | | * </p>
|
| | | * |
| | | * @param data
|
| | | * @return
|
| | | */
|
| | | public static String objectToJson(Object data) {
|
| | | String string = baseGson.toJson(data);
|
| | | return string;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 将json结果集转化为对象
|
| | | * |
| | | * @param jsonData
|
| | | * json数据
|
| | | * @param clazz
|
| | | * 对象中的object类型
|
| | | * @return
|
| | | */
|
| | | public static <T> T jsonToObject(String jsonData, Class<T> beanType) {
|
| | | return baseGson.fromJson(jsonData, beanType);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 将json数据转换成pojo对象list
|
| | | * <p>
|
| | | * Title: jsonToList
|
| | | * </p>
|
| | | * <p>
|
| | | * Description:
|
| | | * </p>
|
| | | * |
| | | * @param jsonData
|
| | | * @param beanType
|
| | | * @return
|
| | | */
|
| | | public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
|
| | | if (StringUtil.isNullOrEmpty(jsonData))
|
| | | return null;
|
| | | List<T> list = new ArrayList<>();
|
| | | JSONArray array = JSONArray.fromObject(jsonData);
|
| | | for (int i = 0; i < array.size(); i++) {
|
| | | try {
|
| | | list.add(baseGson.fromJson(array.optJSONObject(i).toString(), beanType));
|
| | | } catch (Exception e) {
|
| | | list.add(baseGson.fromJson(array.optString(i), beanType));
|
| | | }
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
|
| | | }
|