Java tutorial
/* * Copyright (C) 2016 android@19code.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gmx.library; import com.google.gson.Gson; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import java.lang.reflect.Array; import java.util.Collection; import java.util.Map; public class JsonUtils { public static String toJson(Object obj) { Gson gson = new Gson(); return gson.toJson(obj); } public static <T> T fromJson(String str, Class<T> type) { Gson gson = new Gson(); return gson.fromJson(str, type); } public static JSONObject map2Json(Map<?, ?> data) { JSONObject object = new JSONObject(); for (Map.Entry<?, ?> entry : data.entrySet()) { String key = (String) entry.getKey(); if (key == null) { throw new NullPointerException("key == null"); } try { object.put(key, wrap(entry.getValue())); } catch (JSONException e) { e.printStackTrace(); } } return object; } public static JSONArray collection2Json(Collection<?> data) { JSONArray jsonArray = new JSONArray(); if (data != null) { for (Object aData : data) { jsonArray.put(wrap(aData)); } } return jsonArray; } public static JSONArray object2Json(Object data) throws JSONException { if (!data.getClass().isArray()) { throw new JSONException("Not a primitive data: " + data.getClass()); } final int length = Array.getLength(data); JSONArray jsonArray = new JSONArray(); for (int i = 0; i < length; ++i) { jsonArray.put(wrap(Array.get(data, i))); } return jsonArray; } private static Object wrap(Object o) { if (o == null) { return null; } if (o instanceof JSONArray || o instanceof JSONObject) { return o; } try { if (o instanceof Collection) { return collection2Json((Collection<?>) o); } else if (o.getClass().isArray()) { return object2Json(o); } if (o instanceof Map) { return map2Json((Map<?, ?>) o); } if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long || o instanceof Short || o instanceof String) { return o; } if (o.getClass().getPackage().getName().startsWith("java.")) { return o.toString(); } } catch (Exception ignored) { } return null; } public static JSONObject string2JSONObject(String json) { JSONObject jsonObject = null; try { JSONTokener jsonParser = new JSONTokener(json); jsonObject = (JSONObject) jsonParser.nextValue(); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } }