一.前台转换
1.将json对象转为字符串:var obj ={}; obj.pro1='11111'; obj.pro2='22222'; var str = JSON.stringify(obj);console.info(str);
2.将字符串转为json对象: var obj = JSON.parse(str);console.info(str); 非严格可用 eval(str);
二.后台转换(com.alibaba.fastjson.JSON 部分详见JSON.java)
<span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">import</span></span><span style="font-size:13px"> com.alibaba.fastjson.JSON;
</span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> parse(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">// 把JSON文本parse为JSONObject或者JSONArray
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> JSONObject parseObject(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">// 把JSON文本parse成JSONObject
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> <T> T parseObject(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text, </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Class</span></span><span style="font-size:13px"><T> clazz); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">// 把JSON文本parse为JavaBean
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> JSONArray parseArray(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">// 把JSON文本parse成JSONArray
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> <T> List<T> parseArray(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text, </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Class</span></span><span style="font-size:13px"><T> clazz); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">//把JSON文本parse成JavaBean集合
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> toJSONString(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> object); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">// 将JavaBean序列化为JSON文本
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> toJSONString(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> object, </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">boolean</span></span><span style="font-size:13px"> prettyFormat); </span><span class="code-comment" style="background-color:inherit"><span style="font-size:13px">// 将JavaBean序列化为带格式的JSON文本
</span></span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> toJSON(</span><span class="code-object" style="background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> javaObject); 将JavaBean转换为JSONObject或者JSONArray。</span>
等。
public static String getJsonString(Object obj){
return JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss");
}
public static List parseArray(String listString,Class c){
if(StringUtils.isNotBlank(listString))
return JSON.parseArray(listString, c);
else
return null;
}
写出:
public static String writeJsonObj(Object obj,HttpServletResponse response) throws IOException{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss");
json = json.replaceAll("\t", "");
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
return null;
}