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

      

     

  • 相关阅读:
    IOS中多版本,多设备类型支持注意事项
    runtime
    网络搜集各种iOS开源类库
    Foudation框架常用结构体和常用类
    iOS开发-正则表达式的使用方法
    Github上的iOS资料-个人记录
    iOS应用程序多语言本地化
    星期判断
    【设定本地通知为周一到周五提醒, 周末不提醒解决办法】
    iOS9 HTTPS解决办法
  • 原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9933649.html
Copyright © 2020-2023  润新知