zoukankan      html  css  js  c++  java
  • 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"));
    		}
    	}
    }
    

      

     

  • 相关阅读:
    MVVM知识库总结
    C#常用类汇总
    silverlight调用MVC WebApi方法
    IE调试方法(一)<转>
    PHP中关于超链接的拼接问题
    intval()和(int)转换使用与区别
    ThinkPHP模板(一)
    修改ThinkSNS网站入口
    js中的this和apply
    Thinkphp的Volist标签
  • 原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9933649.html
Copyright © 2011-2022 走看看