• fastJson工具类


    jar:fast.jar

    依赖:

    <!-- fastjson -->
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>fastjson</artifactId>
    	<version>1.2.46</version>
    </dependency>

    工具类

    package json;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import json.test.Student;
    
    public class JSONSerial {
    
    	public static JSONArray toJsonArray(String str) {
    		JSONArray jsonArray = JSON.parseArray(str);
    		return jsonArray;
    	}
    
    	public static JSONObject toJson(String str) {
    		JSONObject jsonObject = JSON.parseObject(str);
    		return jsonObject;
    	}
    
    	public static String serial(Object obj) {
    		return JSON.toJSONString(obj);
    	}
    
    	public static <T> T deserial(String str, Class<T> clazz) {
    		if (str == null || str.length() == 0) {
    			return null;
    		}
    		return JSON.parseObject(str, clazz);
    	}
    
    	public static void main(String[] args) {
    		String jsonStr = "{"studentName":"lily","studentAge":12}";
    //		JSONObject j = toJson(jsonStr);
    //		System.out.println(j.getString("studentName"));
    		
    		Student s = deserial(jsonStr, Student.class);
    		System.out.println(s.getStudentAge());
    		String strs = serial(s);
    		System.out.println(strs);
    		
    		String  JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]";
    		JSONArray jsonArray	= toJsonArray(JSON_ARRAY_STR);
    		for(Object obj : jsonArray){
    			JSONObject jsonObject = (JSONObject) obj;
    			System.out.println(jsonObject.get("studentName"));
    		}
    	}
    }
    

    测试类

    package json.test;
    
    public class Student {
    	private String studentName;
    	private int studentAge;
    	public String getStudentName() {
    		return studentName;
    	}
    	public void setStudentName(String studentName) {
    		this.studentName = studentName;
    	}
    	public int getStudentAge() {
    		return studentAge;
    	}
    	public void setStudentAge(int studentAge) {
    		this.studentAge = studentAge;
    	}
    	
    }
    

    测试执行类

    package json.test;
    
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import json.JSONSerial;
    
    /**  
      * @Title: TestFastJson.java
      * @Package json.test
      * @Description: TODO(用一句话描述该文件做什么)
      * @author licy
      * @date 2018年11月9日
      * @version V1.0  
      */
    
    public class TestFastJson {
    	public static void main(String[] args) {
    		String jsonStr = "{"studentName":"lily","studentAge":12}";
    //		JSONObject j = toJson(jsonStr);
    //		System.out.println(j.getString("studentName"));
    		
    		Student s = JSONSerial.deserial(jsonStr, Student.class);
    		System.out.println(s.getStudentAge());
    		String strs = JSONSerial.serial(s);
    		System.out.println(strs);
    		
    		String  JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]";
    		JSONArray jsonArray	= JSONSerial.toJsonArray(JSON_ARRAY_STR);
    		for(Object obj : jsonArray){
    			JSONObject jsonObject = (JSONObject) obj;
    			System.out.println(jsonObject.get("studentName"));
    		}
    	}
    }
    

      

     

  • 相关阅读:
    uboot的Makefile分析
    S3C2440上触摸屏驱动实例开发讲解(转)
    linux嵌入式驱动软件开发(csdnblog)
    [连接]研究MSN的一些参考资料(MSNP15)
    关于Asp.net中使用以下代码导出Excel表格的问题
    FCKEditor在Asp.net的安装
    奥运后,接手两个项目,PECT培训,CIW培训,系分考试...........一堆流水帐
    [转]甩掉数据字典,让Sql Server数据库也来一个自描述
    SQL Server 调优将会用到的非常好的DMV
    SQL Server 监视数据文件大小变化
  • 原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9933649.html
Copyright © 2020-2023  润新知