转载 解析json之net.sf.json https://blog.csdn.net/itlwc/article/details/38442667

一、介绍

使用之前需要导入的jar包:

json-lib-2.4-jdk15.jar
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar <!--这个包我没导入,也可以使用-->

二、使用

1.JSONObject的使用

public class JSONObjectUse {

    public static void main(String[] args) {
//创建JSONObject对象
JSONObject jsonObject = new JSONObject();
jsonObject.put("username","wln");
jsonObject.put("password","123");
System.out.println("1:" + jsonObject); //增加属性
jsonObject.element("sex","男");
System.out.println("2:" + jsonObject); //判断输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("3:" + "是否是数组:" + isArray +" 是否是空:" + isEmpty + " 是否是空对象:" + isNullObject); //创建JSONArray
JSONArray jsonArray = new JSONArray();
jsonArray.add(0,"aa");
jsonArray.add("BB");
jsonArray.add(1,"AB");
jsonArray.add("cc"); //将JSONArray 添加到JSONObject
jsonObject.element("student",jsonArray);
System.out.println("4:" + jsonObject);
}
} 结果:
1:{"username":"wln","password":"123"}
2:{"username":"wln","password":"123","sex":"男"}
3:是否是数组:false 是否是空:false 是否是空对象:false
4:{"username":"wln","password":"123","sex":"男","student":["aa","AB","BB","cc"]}

2.JSONArray的使用

public class JSONArrayUse {

    public static void main(String[] args) {
//创建JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0,"aa");
jsonArray.add(1,"BB");
jsonArray.element("cc");
jsonArray.add("DD");
System.out.println("1:" + jsonArray);
//根据下标获取数据
System.out.println("2:" + jsonArray.get(0)); //根据下标设置数据
jsonArray.set(0,"AAA");
System.out.println("3:" + jsonArray); //创建JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "lwc");
jsonObject.put("password", "123");
//把JSONObject放入到JSONArray中
jsonArray.add(jsonObject);
System.out.println("4:" + jsonArray); //遍历
System.out.println("5:");
for(int i=0;i<jsonArray.size();i++){
System.out.print(jsonArray.get(i)+"\t");
}
}
} 结果:
1:["aa","BB","cc","DD"]
2:aa
3:["AAA","BB","cc","DD"]
4:["AAA","BB","cc","DD",{"username":"lwc","password":"123"}]
5:
AAA BB cc DD {"username":"lwc","password":"123"}

3.JavaBean与JSON字符串之间的转换

public class JavaBeanUse {

    public static void main(String[] args) {
//将JavaBean转换为JSONObject
Student student = new Student("wln", "22");
JSONObject jsonObject = JSONObject.fromObject(student);
System.out.println("1:" + jsonObject); //将JSONString转换为JSONObject
String jsonStr = "{\"name\":\"nana\",\"age\":\"33\"}";
JSONObject jsonObject1 = JSONObject.fromObject(jsonStr); Student stu = (Student) JSONObject.toBean(jsonObject1,Student.class);
System.out.println("2:" + stu);
System.out.println("3:" + stu.getName() +" "+ stu.getAge());
}
} 结果:
1:{"age":"22","name":"wln"}
2:com.springboot.devtools.jsonUse.entity.Student@61a485d2
3:nana 33
public class Student {

    private String name;
private String age; public Student() {
} public Student(String name, String age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
}
}

4.List与JSON字符串之间的转换

public class ListUse {
public static void main(String[] args) {
//将List转成JSONArray
List list = new ArrayList();
list.add(new Student("wln","22"));
list.add(new Student("nana","33"));
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println("1:" + jsonArray); //将JSONString转成List
List list1 = new ArrayList();
String strJSON = "[{\"name\":\"dada\",\"age\":\"44\"},{\"name\":\"xiaoxiao\",\"age\":\"55\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(strJSON);
for(int i = 0;i < jsonArray1.size(); i++) {
JSONObject jsonObject = jsonArray1.getJSONObject(i);
Student stu = (Student) JSONObject.toBean(jsonObject,Student.class);
list1.add(stu);
}
System.out.println("2:" + list1);
}
} 结果:
1:[{"age":"22","name":"wln"},{"age":"33","name":"nana"}]
2:[com.springboot.devtools.jsonUse.entity.Student@7946e1f4, com.springboot.devtools.jsonUse.entity.Student@3c09711b]

5.Map与JSON字符串之间的转换

public class MapUse {

    public static void main(String[] args) {
//Map转JSONString
Map map = new HashMap();
map.put("1", new Student("wln","22"));
map.put("2", new Student("nana","33"));
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println("1:" + map); //JSONString转Map
String jsonStr = "{\"1\":{\"name\":\"dada\",\"age\":\"44\"},\"2\":{\"name\":\"xiaoxiao\",\"age\":\"55\"}}";
Map map1 = (Map) JSONObject.fromObject(jsonStr);
Set set = map1.keySet();
Iterator ite = set.iterator();
while (ite.hasNext()) {
String key = (String) ite.next();
JSONObject jsonObject = JSONObject.fromObject(map1.get(key));
Student stu = (Student) JSONObject.toBean(jsonObject, Student.class);
System.out.println("2:" + key + " " + stu);
}
}
} 结果:
1:{1=com.springboot.devtools.jsonUse.entity.Student@1b40d5f0, 2=com.springboot.devtools.jsonUse.entity.Student@ea4a92b}
2:1 com.springboot.devtools.jsonUse.entity.Student@3c09711b
2:2 com.springboot.devtools.jsonUse.entity.Student@5cc7c2a6

6.JSONArray与List之间的转换

public class JSONArrayToList {
public static void main(String[] args) {
//List转换为JSONArray
List<Student> list = new ArrayList<Student>();
list.add(new Student("wln","22"));
list.add(new Student("nana","33"));
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println("1:" + jsonArray); //JSONArray转换为List
List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
Iterator<Student> ite = list2.iterator();
while (ite.hasNext()) {
Student stu = ite.next();
System.out.println("2:" + stu);
}
}
} 结果:
1:[{"age":"22","name":"wln"},{"age":"33","name":"nana"}]
2:com.springboot.devtools.jsonUse.entity.Student@69d9c55
2:com.springboot.devtools.jsonUse.entity.Student@13a57a3b

7.JSONArray与数组之间的转换

public class JSONArrayToArray {
public static void main(String[] args) {
//Java数组转换JSONArray
boolean[] boolArray = new boolean[] {true, false, true};
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println("1:"+ jsonArray.toString()); //JSONArray转换Java数组
Object obj[] = jsonArray.toArray();
for (Object o : obj) {
System.out.print("2:" + o + " ");
}
} } 结果:
1:[true,false,true]
2:true 2:false 2:true

8.XML与JSON之间的转换

需要导入xom-1.1.jar

public class XMLToJSON {
public static void main(String[] args) throws Exception {
//XML转换JSON
String xml = "<root>" + "<name type='type'>wln</name>"
+ "<gender>woman</gender>" + "<birthday>" + "<year>1970</year>"
+ "<month>12</month>" + "<day>17</day>" + "</birthday>"
+ "</root>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);
System.out.println("1:" + json.toString(2)); //JSON转换XML
String jsonStr = "{\"root\":{" + "\"name\":\"wln\","
+ "\"gender\":\"woman\"," + "\"birthday\":{"
+ "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\""
+ "}" + "}" + "}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
String xmlStr = new XMLSerializer().write(jsonObject);
System.out.println("2:" + xmlStr);
}
} 结果:
1:{
"name": "wln",
"gender": "woman",
"birthday": {
"year": "1970",
"month": "12",
"day": "17"
}
}
2:<?xml version="1.0" encoding="UTF-8"?>
<o><root class="object"><birthday class="object"><day type="string">17</day><month type="string">12</month><year type="string">1970</year></birthday><gender type="string">woman</gender><name type="string">wln</name></root></o>

JSON数据转换之net.sf.json包的使用的更多相关文章

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

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

  2. 解析json方式之net.sf.json

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

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

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

  4. 使用Gson进行json数据转换(list to json 和json to list)

    文章借鉴自:http://blog.csdn.net/binyao02123202/article/details/7540407 下面是一个简单的例子: Java代码 public class Pe ...

  5. Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)

    目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充.............. ...

  6. json学习初体验--第三者jar包实现bean、List、map创json格式

    1.的需要jar包裹json-lib.jar 下载链接: http://sourceforge.net/projects/json-lib/files/json-lib/ 此包还须要下面的依赖包, c ...

  7. net.sf.json和 com.fasterxml.jackson中对象转json的区别

    近期做项目的时候,发现使用net.sf.json包中的JSONObject或JSONArray将对象转为json数据结构存在一个坑.当对String类型的属性赋值为null情况下,转为json结构为& ...

  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. git flow强制重新初始化

    Gitflow工作流定义了一个围绕项目发布的严格分支模型. git flow初始化命令: git flow init 关于各个分支的命名一路回车就可以了,如果不小心修改了默认的分支命名,后来又觉得不爽 ...

  2. Vue指令(四)--v-model

    1.v-model的使用场景 1.v-model的使用,用于表单控件的数据绑定 2.v-model与value共同使用,实现选项框的选中事件,两者相同时,选中 3.v-model 与v-bind:va ...

  3. Azure 认知服务--计算机视觉 API - 分析图像

    在本节中,笔者将详细介绍 Azure 认知服务中的一种:计算机视觉 (Computer Vision) API. 我的一个客户有需求,他们需要消费者与自己的产品合照,然后上传到服务器并转发到朋友圈. ...

  4. ActiveMQ整合spring结合项目开发流程(生产者和消费者)总结

    一:生产者代码编写: 1.配置pom.xml引入相关坐标 <dependencies> <!-- spring开发测试 --> <dependency> <g ...

  5. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  6. 流畅的python和cookbook学习笔记(九)

    1.减少可调用对象的参数个数,使用functools.partial冻结参数 使用functools.partial(),可以固定一个或者多个值,减少调用参数. >>> def sp ...

  7. thrift简介

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...

  8. MySql数据快速导入

    使用LOAD DATA INFILE 插入速度可以提升很多 左侧是直接导入100W花费135s ,Dos界面通过Load方式导入450W只用时23s,性能一下子显示出来了.

  9. 设计模式入门,观察者模式,c++代码实现

    // test02.cpp : Defines the entry point for the console application.////设计模式第2章 观察者模式#include " ...

  10. mybatis笔记<一> Demo

    mybatis作为一个orm互联网公司基本都在用,今天写个笔记.记录一下mybatis使用 参考官网:http://www.mybatis.org/mybatis-3/getting-started. ...