前面转载了json解析的技术:fastjson,今天说下另外一种技术。
下载地址
  1. 本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
  2. 最新版本:http://sourceforge.net/projects/json-lib/files/json-lib/

使用net.sf.json需要导入的包

JSONObject

  1. package com.itlwc.test;
  2. import net.sf.json.JSONArray;
  3. import net.sf.json.JSONObject;
  4. public class Test {
  5. public static void main(String[] args) {
  6. // 创建JSONObject
  7. JSONObject jsonObject = new JSONObject();
  8. jsonObject.put("username", "lwc");
  9. jsonObject.put("password", "123");
  10. // 打印:1
  11. System.out.println(jsonObject);
  12. // 增加属性,打印:2
  13. jsonObject.element("sex", "男");
  14. System.out.println(jsonObject);
  15. // 根据key返回,打印:3
  16. System.out.println(jsonObject.get("sex"));
  17. // 判读输出对象的类型
  18. boolean isArray = jsonObject.isArray();
  19. boolean isEmpty = jsonObject.isEmpty();
  20. boolean isNullObject = jsonObject.isNullObject();
  21. // 打印:4
  22. System.out.println("是否数组:" + isArray + " 是否空:" + isEmpty + " 是否空对象:"
  23. + isNullObject);
  24. // 把JSONArray增加到JSONObject中
  25. JSONArray jsonArray = new JSONArray();
  26. jsonArray.add(0, "lwc");
  27. jsonArray.add(1, "nxj");
  28. // 开始增加
  29. jsonObject.element("student", jsonArray);
  30. // 打印:5
  31. System.out.println(jsonObject);
  32. }
  33. }
  34. /*
  35. 打印结果
  36. {"username":"lwc","password":"123"}
  37. {"username":"lwc","password":"123","sex":"男"}
  38. 是否为数组:false 是否为空:false 是否为空对象:false
  39. {"username":"lwc","password":"123","sex":"男","student":["lwc","nxj"]}
  40. */

JSONArray

  1. package com.itlwc.test;
  2. import net.sf.json.JSONArray;
  3. import net.sf.json.JSONObject;
  4. public class Test {
  5. public static void main(String[] args) {
  6. //创建JSONArray
  7. JSONArray jsonArray = new JSONArray();
  8. jsonArray.add(0, "lwc");
  9. jsonArray.add(1, "nxj");
  10. jsonArray.element("mxj");
  11. //打印:1
  12. System.out.println(jsonArray);
  13. //根据下标返回,打印:2
  14. System.out.println(jsonArray.get(0));
  15. //根据下标设置新值,打印:3
  16. jsonArray.set(0, "itlwc");
  17. System.out.println(jsonArray);
  18. //把JSONObject放入到JSONArray中
  19. JSONObject jsonObject = new JSONObject();
  20. jsonObject.put("username", "lwc");
  21. jsonObject.put("password", "123");
  22. //开始增加,打印:4
  23. jsonArray.add(jsonObject);
  24. System.out.println(jsonArray);
  25. //遍历,打印:5
  26. for(int i=0;i<jsonArray.size();i++){
  27. System.out.print(jsonArray.get(i)+"\t");
  28. }
  29. }
  30. }
  31. /*
  32. 打印结果
  33. ["lwc","nxj","mxj"]
  34. lwc
  35. ["itlwc","nxj","mxj"]
  36. ["itlwc","nxj","mxj",{"username":"lwc","password":"123"}]
  37. itlwc   nxj mxj {"username":"lwc","password":"123"}
  38. */

JavaBean与json字符串互转

  1. package com.itlwc.test;
  2. import net.sf.json.JSONObject;
  3. import com.itlwc.entity.Student;
  4. public class Test {
  5. public static void main(String[] args) {
  6. // JavaBean对象转json字符串
  7. Student stu1 = new Student("lwc", "111111");
  8. //这个方法是将对象转为json对象(不论是将json字符串还是java对象都是这个方法)
  9. JSONObject jsonObject = JSONObject.fromObject(stu1);
  10. System.out.println(jsonObject);
  11. // json字符串转JavaBean
  12. String jsondata = "{\"password\":\"111111\",\"username\":\"lwc\"}";
  13. JSONObject jsonObject1 = JSONObject.fromObject(jsondata);
  14. Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class);
  15. System.out.println(stu2);
  16. }
  17. }
  18. /*
  19. 打印结果:
  20. {"password":"111111","username":"lwc"}
  21. 用户: lwc 密码:111111
  22. */

List与json字符串互转

  1. package com.itlwc.test;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import net.sf.json.JSONArray;
  5. import net.sf.json.JSONObject;
  6. import com.itlwc.entity.Student;
  7. public class Test {
  8. public static void main(String[] args) {
  9. // List转json字符串
  10. List list = new ArrayList();
  11. list.add(new Student("lwc", "111111"));
  12. list.add(new Student("nxj", "222222"));
  13. //将list集合转为json数组对象
  14. JSONArray jsonArray = JSONArray.fromObject(list);
  15. System.out.println(jsonArray);
  16. // json字符串转List
  17. List list1 = new ArrayList();
  18. String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]";
  19. JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
  20. for (int i = 0; i < jsonArray1.size(); i++) {
  21. JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
  22. Student stu2 = (Student) JSONObject.toBean(jsonObject2,
  23. Student.class);
  24. list1.add(stu2);
  25. }
  26. System.out.println(list1);
  27. }
  28. }
  29. /*
  30. 打印结果:
  31. [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
  32. [用户: lwc 密码:111111, 用户: nxj 密码:222222]
  33. */

Map与json字符串互转

  1. package com.itlwc.test;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import net.sf.json.JSONObject;
  7. import com.itlwc.entity.Student;
  8. public class Test {
  9. public static void main(String[] args) {
  10. // Map转json字符串
  11. Map map = new HashMap();
  12. map.put("1", new Student("lwc", "111111"));
  13. map.put("2", new Student("nxj", "222222"));
  14. JSONObject jsonMap = JSONObject.fromObject(map);
  15. System.out.println(jsonMap);
  16. // json字符串转Map
  17. String jsondata = "{\"2\":{\"password\":\"222222\",\"username\":\"nxj\"},\"1\":{\"password\":\"111111\",\"username\":\"lwc\"}}";
  18. Map map1 = (Map) JSONObject.fromObject(jsondata);
  19. Set set = map1.keySet();
  20. Iterator ite = set.iterator();
  21. while (ite.hasNext()) {
  22. String key = (String) ite.next();
  23. JSONObject jsonObject = JSONObject.fromObject(map1.get(key));
  24. Student stu = (Student) JSONObject
  25. .toBean(jsonObject, Student.class);
  26. System.out.println(key + " " + stu);
  27. }
  28. }
  29. }
  30. /*
  31. 打印结果:
  32. {"2":{"password":"222222","username":"nxj"},"1":{"password":"111111","username":"lwc"}}
  33. 2 用户: nxj 密码:222222
  34. 1 用户: lwc 密码:111111
  35. */

JSONArray与List互转

  1. package com.itlwc.test;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import net.sf.json.JSONArray;
  6. import net.sf.json.JsonConfig;
  7. import com.itlwc.entity.Student;
  8. public class Test {
  9. public static void main(String[] args) {
  10. //List转型JSONArray
  11. List<Student> list = new ArrayList<Student>();
  12. list.add(new Student("lwc", "111111"));
  13. list.add(new Student("nxj", "222222"));
  14. JSONArray jsonArray = JSONArray.fromObject(list);
  15. System.out.println(jsonArray.toString());
  16. //JSONArray转型List
  17. List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
  18. Iterator<Student> ite =  list2.iterator();
  19. while(ite.hasNext()){
  20. Student stu =ite.next();
  21. System.out.println(stu);
  22. }
  23. }
  24. }
  25. /*
  26. 打印结果
  27. [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
  28. 用户: lwc 密码:111111
  29. 用户: nxj 密码:222222
  30. */

JSONArray与数组互转

  1. package com.itlwc.test;
  2. import net.sf.json.JSONArray;
  3. public class Test {
  4. public static void main(String[] args) {
  5. // Java数组转JSONArray
  6. boolean[] boolArray = new boolean[] { true, false, true };
  7. JSONArray jsonArray = JSONArray.fromObject(boolArray);
  8. System.out.println(jsonArray.toString());
  9. // JSONArray转Java数组
  10. Object obj[] = jsonArray.toArray();
  11. for (Object o : obj) {
  12. System.out.print(o + " ");
  13. }
  14. }
  15. }
  16. /*
  17. 打印结果 :
  18. [true,false,true]
  19. true false true
  20. */

XML与JSON互转

需要导入xom-1.1.jar

  1. package com.itlwc.test;
  2. import net.sf.json.JSON;
  3. import net.sf.json.JSONObject;
  4. import net.sf.json.xml.XMLSerializer;
  5. public class Test {
  6. public static void main(String[] args) throws Exception {
  7. // XML转JSON
  8. String xml = "<root>" + "<name type='type'>zhaipuhong</name>"
  9. + "<gender>male</gender>" + "<birthday>" + "<year>1970</year>"
  10. + "<month>12</month>" + "<day>17</day>" + "</birthday>"
  11. + "</root>";
  12. XMLSerializer xmlSerializer = new XMLSerializer();
  13. JSON json = xmlSerializer.read(xml);
  14. System.out.println(json.toString(2));
  15. // JSON转XML
  16. String jsondata = "{\"root\":{" + "\"name\":\"zhaipuhong\","
  17. + "\"gender\":\"male\"," + "\"birthday\":{"
  18. + "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\""
  19. + "}" + "}" + "}";
  20. JSONObject jsonObject = JSONObject.fromObject(jsondata);
  21. String xmlstr = new XMLSerializer().write(jsonObject);
  22. System.out.println(xmlstr);
  23. }
  24. }
  25. /*
  26. 打印结果:
  27. {
  28. "name": "zhaipuhong",
  29. "gender": "male",
  30. "birthday":   {
  31. "year": "1970",
  32. "month": "12",
  33. "day": "17"
  34. }
  35. }
  36. <?xml version="1.0" encoding="UTF-8"?>
  37. <o>
  38. <root class="object">
  39. <birthday class="object">
  40. <day type="string">17</day>
  41. <month type="string">12</month>
  42. <year type="string">1970</year>
  43. </birthday>
  44. <gender type="string">male</gender>
  45. <name type="string">zhaipuhong</name>
  46. </root>
  47. </o>
  48. */

解析json方式之net.sf.json的更多相关文章

  1. java解析JSON (使用net.sf.json)

    例如JSON字符串str如下: {     "data": [         {             "basic_title": "运筹帷幄因 ...

  2. JSON数据转换之net.sf.json包的使用

    转载 解析json之net.sf.json https://blog.csdn.net/itlwc/article/details/38442667 一.介绍 使用之前需要导入的jar包: json- ...

  3. json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...

  4. java中转换json方式(JSONArray,JSONObject),json解析

    package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...

  5. Java中net.sf.json包关于JSON与对象互转的问题

    在Web开发过程中离不开数据的交互,这就需要规定交互数据的相关格式,以便数据在客户端与服务器之间进行传递.数据的格式通常有2种:1.xml:2.JSON.通常来说都是使用JSON来传递数据.本文正是介 ...

  6. Java中net.sf.json包关于JSON与对象互转的坑

    在Web开发过程中离不开数据的交互,这就需要规定交互数据的相关格式,以便数据在客户端与服务器之间进行传递.数据的格式通常有2种:1.xml:2.JSON.通常来说都是使用JSON来传递数据.本文正是介 ...

  7. net.sf.json.JSONObject 和org.json.JSONObject 的差别

    http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建jso ...

  8. net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法

    使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...

  9. net.sf.json 时间格式的转化

    后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...

随机推荐

  1. windows程序内部运行机制

    Windows程序内部运行机制 2007-10-21 19:52 1010人阅读 评论(0) 收藏 举报 windowsvc++applicationcallbackwinapistructure W ...

  2. FPGA算法学习(1) -- Cordic(圆周系统之向量模式)

    旋转模式用来解决三角函数,实现极坐标到直角坐标的转换,基础理论请参考Cordic算法--圆周系统之旋转模式.那么,向量模式则用来解决反三角函数的问题,体现的应用主要是直角坐标向极坐标转换,即已知一点的 ...

  3. 20145202马超 2016-2017-2 《Java程序设计》第一次实验

    之前做的(http://www.cnblogs.com/tuolemi/p/5707098.html) 其余的 断点的使用 行断点 条件断点 参考(http://www.cnblogs.com/roc ...

  4. WPF中,如何将Vista Aero效果扩展到整个窗口

    原文:WPF中,如何将Vista Aero效果扩展到整个窗口   WPF中,如何将Vista Aero效果扩展到整个窗口                                         ...

  5. web框架与爬虫

    所有的web框架 http://www.cnblogs.com/wupeiqi/articles/5341480.html 爬虫技术 http://www.cnblogs.com/wupeiqi/ar ...

  6. 微信营销 推广 会议签到 活动签到 复用微信3D动画签到系统

    适用场合 本软件适合各行各业,尤其世界500强上市公司,推广产品,聚集微信粉丝和人气.如大型展销会,新产品发布,主题活动推广,年会晚会等.各种商业和演出场合. 软件有试用版可供下载试用. 特色功能 顾 ...

  7. Spring常用注解用法总结

    转自http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Dispat ...

  8. 虚拟现实-VR-UE4-编译UE4源码

    通过Git将UE4源代码获取到本地计算机 切记路径不要有中文 这里面我已经在进行编译了,有部分文件是多余出来的, 第一步就是点击 setup.bat批处理,这个过程回取决与你的网速的快慢,我等了一下午 ...

  9. Fiddler 4 实现手机App的抓包

    Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求. Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook ...

  10. Linux-ls,cd,type命令

    windows: dll:dynamic link library,动态链接库 Linux: .so:shared object,共享对象 操作系统: kernel:内核: 1.进程管理 2.内核管理 ...