一、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. 软件产品案例分析——K米

    第一部分 调研.评价 调研 测试机型:iPhone 6s K米版本:4.3.0 直观感受 界面干净,功能丰富,操作不复杂,易上手 错误类 无法分享KTV主页 步骤:进入KTV主页,点击右上角分享图标 ...

  2. 通过Nginx,Tomcat访问日志(access log)记录请求耗时

    一.Nginx通过$upstream_response_time $request_time统计请求和后台服务响应时间 nginx.conf使用配置方式: log_format main '$remo ...

  3. Bois设置教程

    BIOS设置图解教程之Award篇 (目前主板上常见的BIOS主要为AMI与AWARD两个系列,如何辨别BIOS品牌系列请移步,本文详细讲解Award系列的BIOS设置图解教程,如果你的BIOS为AM ...

  4. 单个pdf提取测试

    # -*- coding: utf-8 -*- """ Created on Wed Feb 3 09:32:22 2016 pdf单个文件提取测试 @author: A ...

  5. css 常用代码解析

    .cBan_1 .e2-pro li a{ display: block; -webkit-transition: all 0.3s linear;transition: all 0.3s linea ...

  6. maven test 运行 指定类或方法 打包 mvn clean assembly:assembly

    >mvn test -Dtest=[ClassName] 运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持)   >mvn test ...

  7. 新浪微博客户端(36)-自定义带placeholder的TextView

    iOS 上自带的UITextView竟然不能设置placeholder,但是UITextView却可以,我也真是醉了.没办法了,自己写一个 DJTextView.h #import <UIKit ...

  8. [git]添加项目到git

    写在前面 一直在想把代码托管到git上面,一直没有去研究,最近发现自己写的demo,好多都找不到了,实在是没办法了,耐下心研究了下git.这里通过添加了自己做的demo,算是也是学习下git的操作吧. ...

  9. Linux系统改变ls文件和文件夹颜色方法

    本人之前就针对蓝色文件夹的颜色  我是这样修改的:    cp /etc/DIR_COLORS   ~/.dir_colors vim   ~/.dir_colors   , 将DIR 01;33   ...

  10. Apache ab参数--压力测试

    Apache附带的ab,它非常容易使用,ab可以直接在Web服务器本地发起测试请求.这至关重要,因为我们希望测试的服务器的处理时间,而不包含数据的网络传输时间以及用户PC本地的计算时间. 需要清楚的是 ...