一、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的更多相关文章

  1. Java集合 Json集合之间的转换

    1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...

  2. Java对象 json之间的转换(json-lib)

    在这里主要简单的介绍一下,如何使用json-lib这个工具包来完成Java对象(或集合)与json对象(或集合)之间的转换~ 1. Java对象转换成json(既创建json) 关键类:JSONObj ...

  3. Java 的 JSON 开源类库选择比较(zz)

    在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April  ...

  4. java中json包的使用以及字符串,map,list,自定义对象之间的相互转换

    做一个map和字符串的转换,需要导入这些jar包,这是最基本的一些jar包. 经过多方尝试得出结论入下: 首先导入基本包:json-lib-2.2.3-jdk15.jar 如果没有这个jar包,程序是 ...

  5. java系列--JSON数据的处理

    http://blog.csdn.net/qh_java/article/details/38610599 http://www.cnblogs.com/lanxuezaipiao/archive/2 ...

  6. Java之JSON数据

    特别注意:使用JSON前需要导包 操作步骤地址:http://blog.csdn.net/baidu_37107022/article/details/70876993 1.定义 JSON(JavaS ...

  7. JSON以及Java转换JSON的方法(前后端常用处理方法)

    )); map.put("arr", new String[] { "a", "b" }); map.put("func" ...

  8. java处理json与对象的转化 递归

    整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换! 对于对象json转换中遇到的问题我参考了一篇博客,http://blo ...

  9. 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 ...

  10. Java解析json字符串和json数组

    Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...

随机推荐

  1. NOIp模拟赛 旅游

    很神奇的一道题,金策大爷给的题解: 什么叫神犇什么叫蒟蒻? IOI冠军的一句基本相同让我思考了一下午. 看完了题解我就想都没想开始用遍历二分图搞,但是搞到了65分后就总是会WA掉7组. 然后仔细的看了 ...

  2. 1 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之DRBD的搭建

    preface 近来公司利润上升,购买了10几台服务器,趁此机会,把mysql的主从同步的架构进一步扩展,为了适应日益增长的流量.针对mysql架构的扩展,先是咨询前辈,后和同事探讨,准备采用Mysq ...

  3. Interface/接口

    1. 类和结构能够实现接口 2. 接口声明包含如下四种类型:属性.方法.事件和索引:这些函数声明不能包含任何实现代码,而在每一个成员的主体后必须使用分号 3. 继承接口的类或结构必须实现接口中的所有成 ...

  4. js初学—实现checkbox全选功能

    布局如下: <p ><input type="checkbox" id="che1"/>全选</p><div id=& ...

  5. 数据库SQL优化大总结

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  6. CS架构和BS架构的区别

    C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可以充分利用两端硬件环境的优势. ...

  7. windows7-SQLyog 安装图解

    双击: 双击已下载的SQLyog Enterprise 安装文件,点击“next”,选择“I accept...”,勾选安装组件,选择安装目录,等待安装完成. 协议:选择我接受 选择操作   选择路径 ...

  8. python 五子棋

    http://www.skywind.me/blog/archives/1029 http://blog.csdn.net/skywind/article/details/8164713 https: ...

  9. VC----文件图标和窗口图标及在任务栏显示的图标

    WNDCLASSEX wndcls; wndcls.cbSize=sizeof(wndcls); wndcls.cbClsExtra=0; wndcls.cbWndExtra=0; wndcls.hb ...

  10. linux lsof 用法简介

    1.简介: lsof(list open files)是一个列出当前系统打开文件的工具. 只需输入 lsof 就可以生成大量的信息,因为 lsof 需要访问核心内存和各种文件,所以必须以 root 用 ...