Java转json
一、json介绍
1、 作用:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,是存储和交换文本信息的语法。
2、json以key-value的格式书写,数据间以“,”分开,有两种数据结构:
对象:“{}”括起来的内容
数组:“[]”括起来的内容
如:{
"people":[ {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}, {"firstName":"Jason","lastName":"Hunter","email":"bbbb"}, {"firstName":"Elliotte","lastName":"Harold","email":"cccc"} ] }二、org.json.simple.JSON....与net.sf.json.JSON...的区别
1、json的创建
org.json.simple.JSONObject json = new JSONObject(); json.put(key,value);
net.sf.json.JSONObject json = JSONObject.fromObject(str);
net.sf.json.jsonobject 没有 new JSONObject(String)的构造方法
2、解析基本差不多
注:在封装数据是net.sf.json.JSON...明显高效与org.json.simple.JSON....
三、json需要的包
1、org.json.simple.JSONObject和org.json.simple.JSONArray

2、net.sf.json.JSONObject和net.sf.json.JSONArray

缺commons-lang-2.4.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
缺ezmorph-1.0.6.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
缺commons-logging-1.1.1.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
缺commons-collections-3.1.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
缺commons-beanutils-1.8.0.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
参考自: JSON_百度百科 JSON中国 | JSON中文网 JSON 教程
四、实例介绍
1、JSONtest类
public class JSONtest {
public static void main(String[] args) {
//将Java对象转换成JSONObject对象并输出
javaObjectToJSONObject();
//将javaObjectList转换为JSONArray并输出
javaObjectListToJSONArray();
//将Java对象转换成JSONObject对象并输出
javaObjectToJsonObject();
//将javaObjectList转换为JSONArray并输出
javaObjectListToJsonArray();
//stringArray转换为net.sf.json.JSONArray并输出
stringArrayToJsonArray();
//将stringArray转换为org.json.simple.JSONArray
stringArrayToJSONArray();
}
public static void stringArrayToJSONArray(){
String[] strArr = {"s1","s2","s3"};
org.json.simple.JSONArray jsonArray = new org.json.simple.JSONArray();
for(int i = 0; i < strArr.length; i++){
jsonArray.add(strArr[i]);
}
System.out.println("jsonArray = "+jsonArray);
int len = jsonArray.size();
for(int i = 0; i < len; i++){
System.out.println(jsonArray.get(i));
}
}
输出为:
jsonArray = ["s1","s2","s3"]
s1
s2
s3
public static void stringArrayToJsonArray(){
String[] strArr = {"s1","s2","s3"};
net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(strArr);
System.out.println("jsonArray = "+jsonArray);
int len = jsonArray.size();
for(int i = 0; i < len; i++){
System.out.println(jsonArray.get(i));
}
}
输出为:
jsonArray = ["s1","s2","s3"]
s1
s2
s3
public static void javaObjectListToJsonArray(){
List personList = createPersonList();
net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(personList);
System.out.println("net.sf.json.JSONArray \n jsonArray = "+jsonArray);
int len = jsonArray.size();
for(int i = 0; i < len; i++){
net.sf.json.JSONObject jsonObject = (net.sf.json.JSONObject)jsonArray.get(i);
System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "
+jsonObject.get("age"));
}
}
输出为net.sf.json.JSONArray :
jsonArray = [{"age":0,"id":0,"userName":"shaoyesun0"},{"age":1,"id":1,"userName":"shaoyesun1"},{"age":2,"id":2,"userName":"shaoyesun2"},{"age":3,"id":3,"userName":"shaoyesun3"},{"age":4,"id":4,"userName":"shaoyesun4"},{"age":5,"id":5,"userName":"shaoyesun5"},{"age":6,"id":6,"userName":"shaoyesun6"},{"age":7,"id":7,"userName":"shaoyesun7"},{"age":8,"id":8,"userName":"shaoyesun8"},{"age":9,"id":9,"userName":"shaoyesun9"}]
0 | shaoyesun0 | 0
1 | shaoyesun1 | 1
2 | shaoyesun2 | 2
3 | shaoyesun3 | 3
4 | shaoyesun4 | 4
5 | shaoyesun5 | 5
6 | shaoyesun6 | 6
7 | shaoyesun7 | 7
8 | shaoyesun8 | 8
9 | shaoyesun9 | 9
public static void javaObjectToJsonObject(){
Person person = createPerson();
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(person);
System.out.println("net.sf.json.JSONObject \n jsonObject = "+jsonObject);
System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "
+jsonObject.get("age"));
}
输出为net.sf.json.JSONObject:
jsonObject = {"age":1,"id":1,"userName":"shaoyesun1"}
1 | shaoyesun1 | 1
public static void javaObjectListToJSONArray(){
List personList = createPersonList();
org.json.simple.JSONArray jsonArray = new org.json.simple.JSONArray();
for(Person person : personList){
org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
jsonObject.put("id", person.getId());
jsonObject.put("userName", person.getUserName());
jsonObject.put("age", person.getAge());
jsonArray.add(jsonObject);
}
System.out.println("org.json.simple.JSONArray \n jsonArray = "+jsonArray);
int len = jsonArray.size();
for(int i = 0; i < len; i++){
org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject)jsonArray.get(i);
System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "
+jsonObject.get("age"));
}
}
输出为org.json.simple.JSONArray:
jsonArray = [{"id":0,"userName":"shaoyesun0","age":0},{"id":1,"userName":"shaoyesun1","age":1},{"id":2,"userName":"shaoyesun2","age":2},{"id":3,"userName":"shaoyesun3","age":3},{"id":4,"userName":"shaoyesun4","age":4},{"id":5,"userName":"shaoyesun5","age":5},{"id":6,"userName":"shaoyesun6","age":6},{"id":7,"userName":"shaoyesun7","age":7},{"id":8,"userName":"shaoyesun8","age":8},{"id":9,"userName":"shaoyesun9","age":9}]
0 | shaoyesun0 | 0
1 | shaoyesun1 | 1
2 | shaoyesun2 | 2
3 | shaoyesun3 | 3
4 | shaoyesun4 | 4
5 | shaoyesun5 | 5
6 | shaoyesun6 | 6
7 | shaoyesun7 | 7
8 | shaoyesun8 | 8
9 | shaoyesun9 | 9
public static void javaObjectToJSONObject(){
Person p = createPerson();
org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
jsonObject.put("id", p.getId());
jsonObject.put("userName", p.getUserName());
jsonObject.put("age", p.getAge());
System.out.println("org.json.simple.JSONObject \n jsonObject = "+jsonObject);
System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "
+jsonObject.get("age"));
}
输出为org.json.simple.JSONObject :
jsonObject = {"id":1,"userName":"shaoyesun1","age":1}
1 | shaoyesun1 | 1
public static Person createPerson(){
Person person = new Person();
person.setId(1);
person.setUserName("shaoyesun1");
person.setAge(1);
return person;
}
public static List createPersonList(){
List personList = new ArrayList();
for(int i = 0; i < 10; i++){
Person person = new Person();
person.setId(i);
person.setUserName("shaoyesun"+i);
person.setAge(i);
personList.add(person);
}
return personList;
}
}
2、Person类
public class Person {
private long id;
private String userName;
private int age;
}
Java转json的更多相关文章
- Java集合 Json集合之间的转换
1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...
- Java对象 json之间的转换(json-lib)
在这里主要简单的介绍一下,如何使用json-lib这个工具包来完成Java对象(或集合)与json对象(或集合)之间的转换~ 1. Java对象转换成json(既创建json) 关键类:JSONObj ...
- Java 的 JSON 开源类库选择比较(zz)
在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April ...
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
做一个map和字符串的转换,需要导入这些jar包,这是最基本的一些jar包. 经过多方尝试得出结论入下: 首先导入基本包:json-lib-2.2.3-jdk15.jar 如果没有这个jar包,程序是 ...
- java系列--JSON数据的处理
http://blog.csdn.net/qh_java/article/details/38610599 http://www.cnblogs.com/lanxuezaipiao/archive/2 ...
- Java之JSON数据
特别注意:使用JSON前需要导包 操作步骤地址:http://blog.csdn.net/baidu_37107022/article/details/70876993 1.定义 JSON(JavaS ...
- JSON以及Java转换JSON的方法(前后端常用处理方法)
)); map.put("arr", new String[] { "a", "b" }); map.put("func" ...
- java处理json与对象的转化 递归
整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换! 对于对象json转换中遇到的问题我参考了一篇博客,http://blo ...
- Java JWT: JSON Web Token
Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand libr ...
- Java解析json字符串和json数组
Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...
随机推荐
- Beta版本冲刺第四天 12.10
一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 日期合理性的判断,一个是用户反馈的查看 管 ...
- 通过rsync搭建一个远程备份系统(一)
前言 我公司是电子商务公司,全部是linux系统,每天的网站数都在增加,为了保证安全,需要建立一个远程容灾系统,将网站数据每天凌晨1点备份到远程服务器上,由于数据量大,每天进行进行增量备份,仅仅备份当 ...
- POJ2187Beauty Contest(任意点的最远距离 + 凸包)
题目链接 题意:就是给N个点的坐标,然后求任意两个点距离的平方最大的值 枚举超时. 当明白了 最远距离的两个点一定在凸包上,一切就好办了.求出凸包,然后枚举 #include <iostream ...
- Objective -C学习笔记 之copy(复制)
//自定义类对象实现copy需要遵守copy协议(否则程序崩溃),实现必须实现的协议方法,里面的代码就决定了你的copy是深是浅 #import <Foundation/Foundation.h ...
- liunx 的 grep命令(转载)
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- 原创最简单的ORM例子
这个仅是为了培训做的一个小例子 public class DB { public static string GetClassName(Type type) { if (type == nul ...
- jpa datasource config
application.properties spring.datasource.driverClassName= spring.datasource.url= spring.datasource.u ...
- win7下如何建立ftp服务器
前段时间正在做一个项目,需要上传东西到ftp服务器,纠结于如何建立ftp服务器.经过一番摸索.终于成功建立ftp服务器.现将我的经验跟大家分享一下.不足之处还望多多指点! 步骤/方法 首先在本地机器上 ...
- MySQL学习笔记——存储过程
- texlive2015+texstudio
编译器 texlive2015 编辑器 texstudio