fastjson来做Json转换例子
2015-07-04 14:57
651 查看
不多说废话了,比较简单,直接上代码吧,都是在本人电脑上跑过的。
说明下还有个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
相关文章推荐
- 使用fastjson转换json的简单例子
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
- com.alibaba.fastjson把JSONObject转换为Map<String, String>对象
- Fastjson将list集合转换成json串返回给前台页面
- 【web开发】fastjackson转换json字符串为bean时忽略未知字段
- spring mvc fastJson 自定义类型转换(返回数据) 实现对ObjectId类型转换
- FastJson--阿里开源的速度最快的Json和对象转换工具 https://www.cnblogs.com/kaituorensheng/p/8082631.html
- 使用json-lib把JSON对象转换成java bean list的例子
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
- 使用fastJson进行String与Object之间的转换
- fastjson转换json格式数据为List<HashMap>转换异常问题
- 阿里巴巴的FastJSON解析JSON例子,超级好用。
- FastJson、Jackson、Gson进行Java对象转换Json的细节处理
- 文件内容在Js(Jquery)中,字符串与JSON格式互相转换的示例(直接运行例子)
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
- FastJson--阿里开源的速度最快的Json和对象转换工具
- fastjson 转换 map 为 object 注意事项
- fastJson数据格式转换(二)SerializerFeature属性再解
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具