下载地址

[plain] view plain copy

 
  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

[java] view plain copy

 
  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

[java] view plain copy

 
  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字符串互转

[java] view plain copy

 
  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. JSONObject jsonObject = JSONObject.fromObject(stu1);
  9. System.out.println(jsonObject);
  10. // json字符串转JavaBean
  11. String jsondata = "{\"password\":\"111111\",\"username\":\"lwc\"}";
  12. JSONObject jsonObject1 = JSONObject.fromObject(jsondata);
  13. Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class);
  14. System.out.println(stu2);
  15. }
  16. }
  17. /*
  18. 打印结果:
  19. {"password":"111111","username":"lwc"}
  20. 用户: lwc 密码:111111
  21. */

List与json字符串互转

[java] view plain copy

 
  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. JSONArray jsonArray = JSONArray.fromObject(list);
  14. System.out.println(jsonArray);
  15. // json字符串转List
  16. List list1 = new ArrayList();
  17. String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]";
  18. JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
  19. for (int i = 0; i < jsonArray1.size(); i++) {
  20. JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
  21. Student stu2 = (Student) JSONObject.toBean(jsonObject2,
  22. Student.class);
  23. list1.add(stu2);
  24. }
  25. System.out.println(list1);
  26. }
  27. }
  28. /*
  29. 打印结果:
  30. [{"password":"111111","username":"lwc"},{"password":"222222","username":"nxj"}]
  31. [用户: lwc 密码:111111, 用户: nxj 密码:222222]
  32. */

Map与json字符串互转

[java] view plain copy

 
  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互转

[java] view plain copy

 
  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与数组互转

[java] view plain copy

 
  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

[java] view plain copy

 
  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. */

net.sf.json------json解析的更多相关文章

  1. java,jquery对json的解析

    json常用于浏览器对服务器的数据传递,所以,我们会经常在浏览器和服务器段对json进行封装和拆装,下面对这些进行简单介绍吧 1,服务器端,也就是java方面,我们用的是 net.sf.json-li ...

  2. plist文件、NSUserDefault 对文件进行存储的类、json格式解析

    ========================== 文件操作 ========================== Δ一 .plist文件 .plist文件是一个属性字典数组的一个文件: .plis ...

  3. [开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 场景模拟 假设由于漏存JD SKU对应的店铺信息.这时我们需要重新完全采集所有 ...

  4. JSON数据解析 基础知识及链接收集

    JSON数据解析学习 JSON介绍 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 是存储和交换文本信息的语法.类似 XML.但是JSON 比 ...

  5. 浅谈JSON数据解析方法

    JSON数据解析 JSON是什么?? 如何把JSON数据解析出来 如何把一个字典转换为JSON JSON详细介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交 ...

  6. JSON数据解析(转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...

  7. JSON数据解析(GSON方式) (转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...

  8. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  9. JSON.parse解析单引号名值对报错

    今天整理代码的时候发现JSON.pare解析时会报了一个错误,而且很难找原因,调试了几分钟没有成功,猜测可能是单双引号引起的错误.修改了单双引号后程序正常运行了,现在记录下这个bug. 关于JSON. ...

  10. Android系列---JSON数据解析

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

随机推荐

  1. 微信小程序之生命周期

    1. 整个小程序生命周期 App({}) //app.js App({ onLaunch: function (options) { // 小程序初始化完成时(全局只触发一次) // 程序销毁(过一段 ...

  2. win7笔记本VirtualBox安装黑苹果MacOS 10.13

    环境 时间:2018.04.09,没有指明时间的教程都是耍流氓 笔记本:某州优雅A460P-i7G D2,4G内存,Intel Core i7-2670QM四核八线程(老笔记本勉强能用),ssd硬盘, ...

  3. 设计模式 笔记 享元模式 Flyweight

    //---------------------------15/04/20---------------------------- //Flyweight 享元模式------对象结构型模式 /* 1 ...

  4. stl源码剖析 详细学习笔记deque(2)

    //---------------------------15/3/13---------------------------- self&operator++() { ++cur; if(c ...

  5. java实现基于关键字的文件夹(文件)的搜索、文件夹(文件)的复制、删除

    最近在做一个项目,需要实现这几项功能,上网查了很多资料,自己研究了好几天终于实现了,现在与大家分享一下. 一.JAVA实现文件夹的搜索   在百度搜索N个技术文章,从哪些大牛们共享的资料中终于写出了我 ...

  6. EF Core 新特性——Owned Entity Types

    Owned Entity Types 首先owned entity type是EF Core 2.0的新特性. 至于什么是owned entity types,可以先把他理解为EF Core官方支持的 ...

  7. Jenkins报表 代码 指标分析

    Jenkins报表 这表现在前面的章节中,也有可用最简单的一种是适用于 JUnit 测试报告的许多报表插件. 在生成后动作进行任何工作,你可以定义要创建的报告. 该构建已经完成,测试结果选项将可进一步 ...

  8. 使用devstack/pike部署多节点实验

    目录 第一步:安装Ubuntu16.04 server并以stack为用户名创建用户 第二步:安装git及相关配置 第三步:安装Open vSwitch 2.5.X 第四步:获取devstack脚本 ...

  9. Shell 基础 -- 总结几种括号、引号的用法

    Shell 脚本中经常需要用到一些括号.引号表达式,功能各不相同,本文详细介绍一下. 1.双引号 " " 双引号常用于包含一组字符串,在双引号中,除了 "$". ...

  10. 利用十字链表压缩稀疏矩阵(c++)-- 数据结构

    题目: 7-1 稀疏矩阵 (30 分)   如果一个矩阵中,0元素占据了矩阵的大部分,那么这个矩阵称为“稀疏矩阵”.对于稀疏矩阵,传统的二维数组存储方式,会使用大量的内存来存储0,从而浪费大量内存.为 ...