json的操作

package com.wfn.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

/**  
* <p>Title: JsonUtils</p>  
* <p>Description: json操作</p>  
* @author liuyawei  
* @date 2019年6月13日  
*/  
public class JsonUtils {

    private final static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    private static XMLSerializer xmlserializer = new XMLSerializer();
    public static Gson getInstance(){
      return gson;
    }
    
    /**  
     * <p>Title: list2Json</p>  
     * <p>Description: list转换json</p>  
     * @param list
     * @return  
     */  
    public static <T> String list2Json(List<T> list) {
        if (null != list && list.size() > 0) {
            JSONArray object = JSONArray.fromObject(list);
            return object.toString();
        }
        return "";
    }
    
    
    /**  
     * <p>Title: json2List</p>  
     * <p>Description: json转换list</p>  
     * @param json
     * @return  
     */  
    public static <T> List<T> json2List(String json){
        if(!StringUtils.isNotBlank(json)){
            JSONArray jsonArray = JSONArray.fromObject(json);
            List<T> list = (List<T>) JSONArray.toCollection(jsonArray);
            return list;
        }
        return null;
    }
    
    /**  
     * <p>Title: obj2Json</p>  
     * <p>Description: 对象转换json</p>  
     * @param obj
     * @return  
     */  
    public static String obj2Json(Object obj){
        if(null != obj){
            JSONArray object = JSONArray.fromObject(obj);
            return object.toString();
        }
        return null;
    }
    
    
    /**  
     * <p>Title: objectToJson</p>  
     * <p>Description: 对象转换json</p>  
     * @param obj
     * @return  
     */  
    public static String objectToJson(Object obj){
        return gson.toJson(obj);
    }
    
    /**  
     * <p>Title: json2Obj</p>  
     * <p>Description: json转换obj</p>  
     * @param json
     * @param objCla
     * @return  
     */  
    public static <T> T json2Obj(String json,Class<T> objCla){
        if(null != json){
            String leftStr = json.substring(0, 2);
            System.out.println(leftStr);
            String rightStr = json.substring(json.length()-2, json.length());
            System.out.println(rightStr);
            if(leftStr.endsWith("[{")){
                json = json.substring(1, json.length());
                System.out.println(json);
            }
            if(rightStr.equals("}]")){
                json = json.substring(0, json.length()-1);
                System.out.println(json);
            }
            JSONObject jsonObject = JSONObject.fromObject(json);
            return (T) JSONObject.toBean(jsonObject,objCla);
            
        }
        return null;
    }
    
    /**  
     * <p>Title: jsonArrayToJSONString</p>  
     * <p>Description: JsonArray 转为 JSON</p>  
     * @param jsonArray
     * @return  
     */  
    public static String jsonArrayToJSONString(JSONArray jsonArray) {
            if(null != jsonArray){
              return jsonArray.toString();
            }
              return "";
        }
    
    /**  
     * <p>Title: jsonObjectToJSONString</p>  
     * <p>Description: JsonObject 转为  JSON</p>  
     * @param jsonObject
     * @return  
     */  
    public static String jsonObjectToJSONString(JSONObject jsonObject) {
            if(null != jsonObject){
              return jsonObject.toString();
            }
              return "";
        }
    
    /**  
     * <p>Title: object2JsonObject</p>  
     * <p>Description: 将Object转换为JsonObject</p>  
     * @param object
     * @return  
     */  
    public static JSONObject object2JsonObject(Object object) {
            if(null != object){
             return JSONObject.fromObject(object);
            }
             return null;
        }
    
    public static String xml2json(String xmlString){
        if(StringUtils.isNotBlank(xmlString)){
        try {
        return xmlserializer.read(xmlString).toString();
        } catch (Exception e) {
        e.printStackTrace();
        return null;
        }
        }
        return null;
        }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值