位置: 编程技术 - 正文
推荐整理分享java中List对象转换为JSON对象(java list转set的方法),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:java把list对象转化为字符串,java把list中元素转换类型,java把list中元素转换类型,java将list转为jsonobject,java将list转为jsonobject,java将list转为jsonobject,java把list对象转化为字符串,java把list对象转化为字符串,内容如对您有帮助,希望把文章链接给更多的朋友!
java中List对象转换为JSON对象
以为兄弟写的!感觉风颇为幽默,自恋的来说,很像我的风哦!呵呵!
好啦!帮他的成果复制过来吧!
一、什么是JSON
JSON 即 JavaScript Object Natation(Java对象表示法),它是一种轻量级的数据交换式,非常适合于服务器与 JavaScript 的交互。
简而言之,JSON就是JavaScript交换数据的一种式。例子如下:
[java] view plaincopyprint?package young.ajax.json; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class JsonUtil { private static Log log = LogFactory.getLog(JsonUtil.class); public static String object2json(Object obj) { StringBuilder json = new StringBuilder(); if (obj == null) { json.append(""""); } else if (obj instanceof String || obj instanceof Integer || obj instanceof Float || obj instanceof Boolean || obj instanceof Short || obj instanceof Double || obj instanceof Long || obj instanceof BigDecimal || obj instanceof BigInteger || obj instanceof Byte) { json.append(""").append(string2json(obj.toString())).append("""); } else if (obj instanceof Object[]) { json.append(array2json((Object[]) obj)); } else if (obj instanceof List) { json.append(list2json((List<?>) obj)); } else if (obj instanceof Map) { json.append(map2json((Map<?, ?>) obj)); } else if (obj instanceof Set) { json.append(set2json((Set<?>) obj)); } else { json.append(bean2json(obj)); } return json.toString(); } public static String bean2json(Object bean) { StringBuilder json = new StringBuilder(); json.append("{"); PropertyDescriptor[] props = null; try { props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors(); } catch (IntrospectionException e) {} if (props != null) { for (int i = 0; i < props.length; i) { try { String name = object2json(props[i].getName()); String value = object2json(props[i].getReadMethod().invoke(bean)); json.append(name); json.append(":"); json.append(value); json.append(","); } catch (Exception e) {} } json.setCharAt(json.length() - 1, '}'); } else { json.append("}"); } return json.toString(); } public static String list2json(List<?> list) { StringBuilder json = new StringBuilder(); json.append("["); if (list != null && list.size() > 0) { for (Object obj : list) { json.append(object2json(obj)); json.append(","); } json.setCharAt(json.length() - 1, ']'); } else { json.append("]"); } return json.toString(); } public static String array2json(Object[] array) { StringBuilder json = new StringBuilder(); json.append("["); if (array != null && array.length > 0) { for (Object obj : array) { json.append(object2json(obj)); json.append(","); } json.setCharAt(json.length() - 1, ']'); } else { json.append("]"); } return json.toString(); } public static String map2json(Map<?, ?> map) { StringBuilder json = new StringBuilder(); json.append("{"); if (map != null && map.size() > 0) { for (Object key : map.keySet()) { json.append(object2json(key)); json.append(":"); json.append(object2json(map.get(key))); json.append(","); } json.setCharAt(json.length() - 1, '}'); } else { json.append("}"); } return json.toString(); } public static String set2json(Set<?> set) { StringBuilder json = new StringBuilder(); json.append("["); if (set != null && set.size() > 0) { for (Object obj : set) { json.append(object2json(obj)); json.append(","); } json.setCharAt(json.length() - 1, ']'); } else { json.append("]"); } return json.toString(); } public static String string2json(String s) { if (s == null) return ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i) { char ch = s.charAt(i); switch (ch) { case '"': sb.append("\""); break; case '\': sb.append("\\"); break; case 'b': sb.append("\b"); break; case 'f': sb.append("\f"); break; case 'n': sb.append("\n"); break; case 'r': sb.append("\r"); break; case 't': sb.append("\t"); break; case '/': sb.append("\/"); break; default: if (ch >= 'u' && ch <= 'uF') { String ss = Integer.toHexString(ch); sb.append("\u"); for (int k = 0; k < 4 - ss.length(); k) { sb.append('0'); } sb.append(ss.toUpperCase()); } else { sb.append(ch); } } } return sb.toString(); } } 这应该是一个高手自己写的代码,就是用刚才第一种方法的人,哈哈!这个类就能将你的几乎所有类型转换成JSON形式的字符串,你可以自己写个类测试一下,我就不写了。 [java] view plaincopyprint?package young.ajax.json;import [java] view plaincopyprint?java.util.HashMap; [java] view plaincopyprint?import java.util.List; [java] view plaincopyprint?import java.util.Map; [java] view plaincopyprint?import net.sf.json.JSONArray; [java] view plaincopyprint?import net.sf.json.JSONException; [java] view plaincopyprint?import net.sf.json.JSONObject; [java] view plaincopyprint?public class JsonConvert [java] view plaincopyprint?{ public static JSONObject message(String message, boolean success) [java] view plaincopyprint?{ Map map = new HashMap(); [java] view plaincopyprint?map.put("success", success); [java] view plaincopyprint?map.put("message", message); [java] view plaincopyprint?return JSONObject.fromObject(map); [java] view plaincopyprint?} [java] view plaincopyprint?public static JSONObject generate(List list) [java] view plaincopyprint?{ Map map = new HashMap(); [java] view plaincopyprint?map.put("totalProperty", list.size()); [java] view plaincopyprint?map.put("root", list); [java] view plaincopyprint?return JSONObject.fromObject(map); [java] view plaincopyprint?} [java] view plaincopyprint?public static JSONObject javabean2json(Object object, String message, boolean success) [java] view plaincopyprint?{ Map map = new HashMap(); [java] view plaincopyprint?map.put("success", success); [java] view plaincopyprint?map.put("message", message); [java] view plaincopyprint?map.put("data", object); [java] view plaincopyprint?return JSONObject.fromObject(map); [java] view plaincopyprint?} [java] view plaincopyprint?public static JSONObject objectcollect2json(List list, String total) { [java] view plaincopyprint?Map map = new HashMap(); [java] view plaincopyprint?map.put("totalProperty", total); [java] view plaincopyprint?map.put("root", list); [java] view plaincopyprint?return JSONObject.fromObject(map); [java] view plaincopyprint?} [java] view plaincopyprint?public static JSONArray getJSONArrayFormString(String str) { [java] view plaincopyprint?if (str == null || str.trim().length() == 0) { [java] view plaincopyprint?return null; [java] view plaincopyprint?} [java] view plaincopyprint?JSONArray jsonArray = null; [java] view plaincopyprint?try { [java] view plaincopyprint?jsonArray = JSONArray.fromObject(str); [java] view plaincopyprint?} catch (JSONException e) { e.printStackTrace(); } [java] view plaincopyprint?return jsonArray; } [java] view plaincopyprint?public static JSONObject StringToJSONOBject(String str) { [java] view plaincopyprint?if (str == null || str.trim().length() == 0) [java] view plaincopyprint?{ return null; } [java] view plaincopyprint?JSONObject jsonObject = null; [java] view plaincopyprint?try { jsonObject = JSONObject.fromObject(str); [java] view plaincopyprint?} catch (JSONException e) { e.printStackTrace(); } [java] view plaincopyprint?return jsonObject; }}Ubuntu 下的Android Studio如何设置主题 在ubuntu下设置androidstudio的主题与windows下相同,具体步骤如下:打开AndroidStudio菜单栏中的File--Settings--Appearance,在右侧你会看到有一个Theme的选项,可以在
电子词典 1、运行效果图2.训练目标掌握AutoCompleteTextView组件的使用3.步骤(1)把原数据库拷贝项目源码的res/raw目录下,然后建立一个DBHelper类(2)使用SQLiteDatabase
Please ensure that adb is correctly located at D:Androidandroid-sdkplatform-toolsadb.exe and 1.启动任务管理器2.找到百度安全组件杀掉进程。3.一般都是组件给禁止了。版权声明:本文为博主原创文章,未经博主允许不得转载。
标签: java list转set的方法
本文链接地址:https://www.jiuchutong.com/biancheng/378062.html 转载请保留说明!上一篇:Android_Intent_文章列表(android.intent.extra.text)
下一篇:Ubuntu 下的Android Studio如何设置主题(ubuntu on android)
友情链接: 武汉网站建设