• 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"));
    		}
    	}
    }
    

      

     

  • 相关阅读:
    概念
    Jquery和Aspnet前台控件及后台代码交互
    未能找到引用的组件“Microsoft.Office.Core
    C#操作Excel,调用ApplicationClass.Quit()关闭Excel时,发生异常:Microsoft Office Word 遇到问题需要关闭
    Javasrcipt捕获按键
    使用Interop.Excel生成Excel
    Javasrcipt时间相关函数
    (转)各种纹理贴图技术
    (转)立体纹理
    (转)地形碰撞高度计算
  • 原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9933649.html
Copyright © 2020-2023  润新知