JsonUtil工具类


1、Json转换成实体

Shop shop = JSONUtil.toBean(shopJson, Shop.class);
	/**
	 * 将Json字符串转换成对应对象
	 * @param jsonString	Json字符串
	 * @param clazz		对应字节码文件.class
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static<T> T jsonToObject(String jsonString, Class<T> clazz){
		if (clazz == String.class) {
			return (T) jsonString;
		} else {
			return (T)gson.fromJson(jsonString, clazz);
		}
	}

2、实体转json字符串

String s = JSONUtil.toJsonStr(shopJson);
	/**
	 * 将一个对象转换成一个Json字符串
	 * @param t
	 * @return
	 */
	public static <T> String objectToJson(T t){
		if (t instanceof String) {
			return t.toString();
		} else {
			return gson.toJson(t);
		}
	}

3、list集合转换成json

	/**
	 * 将List集合转换为json字符串
	 * @param list	List集合
	 * @return
	 */
	public static<T> String listToJson(List<T> list){
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = null;
		try {
			for (int i = 0; i < list.size(); i++) {
				jsonObject = new JSONObject(objectToJson(list.get(i)));
				jsonArray.put(jsonObject);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			if (jsonObject != null) {
				jsonObject = null;
			}
		}
		return jsonArray.toString();
	}

4、数组转换成json

	/**
	 * 将数组转换成json字符串
	 * @param array		数组
	 * @return
	 */
	public static<T> String arrayToJson(T[] array){
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = null;
		try {
			for (int i = 0; i < array.length; i++) {
				jsonObject = new JSONObject(objectToJson(array[i]));
				jsonArray.put(jsonObject);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			if (jsonObject != null) {
				jsonObject = null;
			}
		}
		return jsonArray.toString();
	}

5、json字符串转换为ContentValues

	/**
	 * json字符串转换为ContentValues
	 * @param json	json字符串
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static ContentValues jsonToContentValues(String json){
		ContentValues contentValues = new ContentValues();
		try {
			JSONObject jsonObject = new JSONObject(json);
			Iterator iterator = jsonObject.keys();
			String key;
			Object value;
			while (iterator.hasNext()) {
				key = iterator.next().toString();
				value = jsonObject.get(key);
				String valueString = value.toString();
				if (value instanceof String) {
					contentValues.put(key, valueString);
				}else if(value instanceof Integer){
					contentValues.put(key, Integer.valueOf(valueString));
				}else if(value instanceof Long){
					contentValues.put(key, Long.valueOf(valueString));
				}else if(value instanceof Double){
					contentValues.put(key, Double.valueOf(valueString));
				}else if(value instanceof Float){
					contentValues.put(key, Float.valueOf(valueString));
				}else if(value instanceof Boolean){
					contentValues.put(key, Boolean.valueOf(valueString));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			throw new Error("Json字符串不合法:" + json);
		}
		
		return contentValues;
	}

6、获取jsonObject对象中的值

	/**
	 * 获取jsonObject对象中的值
	 * @param jsonObject	jsonObject对象
	 * @param key	键值
	 * @param clazz	所取数据类型,例如:Integer.class,String.class,Double.class,JSONObject.class
	 * @return  存在则返回正确值,不存在返回null
	 */
	@SuppressWarnings("unchecked")
	public static<T> T getJsonObjectValue(JSONObject jsonObject, String key, Class<T> clazz){
		T t = null;
		try {
			if (clazz == Integer.class) {
				t = (T) Integer.valueOf(jsonObject.getInt(key));
			}else if(clazz == Boolean.class){
				t = (T) Boolean.valueOf(jsonObject.getBoolean(key));
			}else if(clazz == String.class){
				t = (T) String.valueOf(jsonObject.getString(key));
			}else if(clazz == Double.class){
				t = (T) Double.valueOf(jsonObject.getDouble(key));
			}else if(clazz == JSONObject.class){
				t = (T) jsonObject.getJSONObject(key);
			}else if(clazz == JSONArray.class){
				t = (T) jsonObject.getJSONArray(key);
			}else if(clazz == Long.class){
				t = (T) Long.valueOf(jsonObject.getLong(key));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return t;
	}

7、获取json字符串中的值

	/**
	 * 获取json字符串中的值
	 * @param json	json字符串
	 * @param key	键值
	 * @param clazz	所取数据类型,例如:Integer.class,String.class,Double.class,JSONObject.class
	 * @return  存在则返回正确值,不存在返回null
	 */
	public static<T> T getJsonObjectValue(String json, String key, Class<T> clazz){
		try {
			return getJsonObjectValue(new JSONObject(json), key, clazz);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值