不多说废话了,比较简单,直接上代码吧,都是在本人电脑上跑过的。
说明下还有个Student的实体类没上传了,随便搞几个属性测试下就可以。
转换类:
import java.io.IOException; import java.util.Collections; import java.util.List; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class HTTPHelper { private static String jsonStr = "{\"mobilePhone\":\"13510398031\",\"userName\":\"李四\"}"; private static String jsonStr2 = "[{\"mobilePhone\":\"13510398031\",\"userName\":\"李四\"},{\"mobilePhone\":\"13824568145\",\"userName\":\"张三\"}]"; private static final JSON buildJSON(String jsonStr) throws IOException { return JSONSerializer.toJSON(jsonStr); } //字符串 转 JSONObject public static final JSONObject buildJSONObject() throws IOException { return (JSONObject) buildJSON(jsonStr); } //字符串(数组) 转 JSONArray public static final JSONArray buildJSONArray() throws IOException { return (JSONArray) buildJSON(jsonStr2); } // 字符串 转 JSONObject public static final JSONObject buildJSONObjectFromParameter() throws IOException { return JSONObject.fromObject(jsonStr); } // 字符串(数组) 转 JSONArray public static final JSONArray buildJSONArrayFromParameter() throws IOException { return JSONArray.fromObject(jsonStr2); } //Json字符串 转 实体对象 public static final <T> T getObject(Class<T> clazz) throws IOException { return com.alibaba.fastjson.JSONObject.parseObject(jsonStr, clazz); } //Json字符串 转 数组对象 @SuppressWarnings("unchecked") public static final <T> List<T> getList(Class<T> clazz) throws IOException { List<T> list = com.alibaba.fastjson.JSONArray.parseArray(jsonStr2, clazz); return list == null ? Collections.EMPTY_LIST : list; } // 对象 转 Json字符串 public static final String getJsonString(Object o) throws IOException { return com.alibaba.fastjson.JSONObject.toJSONString(o); } // 对象 转 com.alibaba.fastjson.JSONObject对象 public static final com.alibaba.fastjson.JSONObject getJson(Object o) throws IOException { return (com.alibaba.fastjson.JSONObject)com.alibaba.fastjson.JSONObject.toJSON(o); } //数组对象 转 Json字符串 public static final String getJsonArrayString(Object o) throws IOException { return com.alibaba.fastjson.JSONArray.toJSONString(o); } }
测试代码:
import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import com.user.Student; import com.utils.HTTPHelper; public class HTTPHelperTest { public static void main(String[] args) throws IOException { // 字符串 转 JSONObject JSONObject jsonObject = HTTPHelper.buildJSONObjectFromParameter(); String account = jsonObject.getString("mobilePhone"); String name = jsonObject.getString("userName"); System.out.println(account+"======================"+name); // 字符串(数组) 转 JSONArray JSONArray jsonArray = HTTPHelper.buildJSONArrayFromParameter(); System.out.println(jsonArray); Iterator iterator = jsonArray.iterator(); while (iterator.hasNext()) { JSONObject jsonObj = (JSONObject) iterator.next(); System.out.println(jsonObj.getString("userName")); } // 字符串(数组) 转 JSONArray JSONArray jsonArr = HTTPHelper.buildJSONArray(); System.out.println(jsonArr); Iterator iterator1 = jsonArr.iterator(); while (iterator1.hasNext()) { JSONObject jsonObj = (JSONObject) iterator1.next(); System.out.println(jsonObj.getString("userName")); } //Json字符串 转 实体对象 Student s = HTTPHelper.getObject(Student.class); System.out.println(s.getUserName()); //Json字符串 转 数组对象 List<Student> list = HTTPHelper.getList(Student.class); System.out.println(list.size()); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).getUserName()); } // 对象 转 Json字符串 Student stu = new Student(); stu.setAge(45); stu.setBirthDay(new Date()); List<String> hobbiy = new ArrayList<String>(); hobbiy.add("唱歌");hobbiy.add("跳舞");hobbiy.add("爬山"); stu.setHobbiy(hobbiy); stu.setUserName("张三"); System.out.println(HTTPHelper.getJsonString(stu)); // 对象 转 com.alibaba.fastjson.JSONObject对象 com.alibaba.fastjson.JSONObject jsonObj = HTTPHelper.getJson(stu); System.out.println(jsonObj.getString("userName")); //数组对象 转 Json字符串 List<Student> listStudent = new ArrayList<Student>(); listStudent.add(stu); System.out.println(HTTPHelper.getJsonArrayString(listStudent)); } }
使用的主要jar包:
fastjson-1.2.5.jar
json-lib-2.4-jdk15.jar
相关推荐
实现json转换,fastjson json json转换,可以帮助开发中涉及json格式处理的程序猿。
使用fastjson.jar解析复杂的json数据,包含完整实例代码
fastjson 快速解析json json、List互转
通过fastjson实现了JavaBean,list,MAP,list等格式与json格式之间的转换,已经附带fastjson的包,包的版本为fastjson-1.1.36.jar
android 开发中java,json转换包。json转换jar包。
阿里巴巴 fastjson是目前解析json数据的一种比较高效,快速的方式。
java使用fastJson处理复杂Json字符串,直接获取key对应的value值
fastjson 的一个小例子,里面是我博客中的代码, http://blog.csdn.net/yunxiaoxiaoyun/article/details/16811973
fastJSON C#的JSON开发包, 版本1.9.8
fastJson_解析json的利器
fastJSON 版本 v2.1.18
Volley。fastJson解析网络Json ,多线程显示图片,简单缓存图片,万能适配器,完美解决图片排序混乱问题,完美解决图片多次加载问题
比较好用的两个json生产工具,并且一直再用效率高依赖少。
使用阿里巴巴提供的fastjson可以快速的实现java对象和json串之间的相互转换,避免字符串拼接的繁琐!
json解析在android开发中是非常,常见的fastjson是阿里巴巴的一个库,对于json开发效率很高,我写了一个简单例子
1.FastJson的介绍: JSON协议使用方便,越来越流行,...1.JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换 2.JSONObject:fastJson提供的json对象 3.JSONArray:fastJson提供json数组对象
fastjson Json解析
fastjson解析json数据,解析速度快
Java常用四大json库,包含目前最新版gson,fastjson,jackson,json-lib
使用org.json 、谷歌gson 以及阿里巴巴fastjson解析JSON格式数据的完整例子(JavaWeb项目)