JsonUtil json转object,json转list,json转bean等

import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

import com.alibaba.fastjson.JSONObject;

public class JsonUtil {

    /**
     * 对象转JSON字符串
     * @param object
     * @return
     */
    public static String objectToJson(Object object){
        return JSONObject.toJSONString(object);
    }
    
    /**
     * JSON字符串转对象
     * @param jsonStr
     * @param clazz
     * @return
     */
    public static  T jsonStrToObject(String jsonStr, Class clazz){
        return JSONObject.parseObject(jsonStr, clazz);

    }
    
    /**
     * JSON字符串转List
     * @param type
     * @param listString
     * @return
     */
    public static  List listStringToOjectList(Class type,String listString){
        return JSONObject.parseArray(listString, type);

    }
    
    @SuppressWarnings("resource")
    public static String inputStreamToString(InputStream is){
           Scanner scanner = new Scanner(is, "UTF-8");
           String s = scanner.useDelimiter("\\A").next();
           return s;
    }
    
}

你可能感兴趣的:(JsonUtil json转object,json转list,json转bean等)