[JSON教程]Object和JSON互转

来源:互联网 发布:linux网卡配置生效 编辑:程序博客网 时间:2024/06/06 01:54
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class JsonUtils {

/**
* 把object转换成JSON
* @param obj
* @return
*/
public static String toJson(Object obj){
Object objJson= JSONArray.toJSON(obj);
String json=objJson.toString();
return json;
}
/**
* 把JSON转换为OBJECT
* @param json
* @param clazz
* @return
*/

@SuppressWarnings("static-access")
public static <T> T  toObject(String json,Class<T> clazz){
JSONObject obj=new JSONObject();
T body= obj.parseObject(json, clazz);
return body;

}

/**
* 获取属性
* @param json
*/
public static Object getProperty(String json,String propertyName){
JSONObject object = JSON.parseObject(json); 
return object.get(propertyName);
}

public static void main(String[] args){
String str="";
}




}
原创粉丝点击