/// <summary>
/// 把JSON字符串还原为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="szJson">JSON字符串</param>
/// <returns>对象实体</returns>
public T ParseFormJson<T>(string szJson)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
return (T)dcj.ReadObject(ms);
}
}
/**
* 将实体POJO转化为JSON
* @param obj
* @return
* @throws JSONException
* @throws IOException
*/
public
static
<T> JSONObject objectToJson(T obj)
throws
JSONException, IOException {
ObjectMapper mapper =
new
ObjectMapper();
// Convert object to JSON string
String jsonStr =
""
;
jsonStr = mapper.writeValueAsString(obj);
return
new
JSONObject(jsonStr);
}