http://www.360doc.com/content/11/0307/16/1542811_98944616.shtml
http://hybxiaodao.iteye.com/blog/1107651 object --->JSONObject ---->json
http://download.csdn.net/download/w59879213/2994102 json.jar 包下载
http://hi.baidu.com/sharppoint/blog/item/3a991919e4317f7fdab4bd9c.html object ---->JSONObject
xml
public void xxxMethod() throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();
response.setContentType("application/xml;charset=UTF-8");
out.write("XML文档");
}
public void xxxMethod() throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();
response.setContentType("application/xml;charset=UTF-8");
out.write("XML文档");
}
json
在action 写以下代码 向客户端发送 json数据
HttpServletRequest request =ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");//设置utf-8防止显示中文乱码
int bookTypeId = Integer.parseInt(request.getParameter("bookTypeId"));
int num = admimService.getDeleteBookTypeCond(bookTypeId);
response.setContentType(ContentType_JSON);
if(num == 0){
boolean isSuccess = true;
int x = admimService.deleteBookType(bookTypeId);
//这是产生简单的json的方法
response.getWriter().write("{success:"+isSuccess+",num:"+num+"}");
}else{
response.getWriter().write("{success:false,num:"+num+"}");
}
Object 转换成 json
------------------------------------gan-client-------------------------------------------------------
public class JsonToObjoct {
/*
* jsonString json字符串
* listKey jsonArray key
*
* String 换成 jsonArray
* */
public static JSONArray StringToJsonArray (String jsonString,String listKey)
throws JSONException{
try{
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray jsonArray = jsonObj.getJSONArray(listKey);
return jsonArray;
}catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
//json
public static String JsonToString(HashMap<String, Object> paramHashMap) throws JSONException{
JSONObject param = new JSONObject();
// 添加通用参数
if(paramHashMap!=null)
{
Iterator iterator=paramHashMap.entrySet().iterator();
Entry entry=null;
while(iterator.hasNext())
{
entry=(Entry) iterator.next();
param.put((String) entry.getKey(), entry.getValue());
}
}
String jsonContent = param.toString();
return jsonContent;
}
//json 转换成 object
public static List<Object> JsonToObject (JSONArray jsonArray)
throws JSONException {
try {
int size = jsonArray.length();
List<Object> typeList = new ArrayList<Object>(size);
for (int i = 0; i < size; i++) {
typeList.add(jsonArray.getJSONObject(i));
}
return typeList;
} catch (JSONException je) {
je.printStackTrace();
}
return null;
}
//object 转成 jsonObject,jsonObject的名字取值
public static List<JSONObject> ObjectToJson(HashMap<String, Object> paramHashMap,String objectName)throws JSONException
{
List<JSONObject> jsonList = new ArrayList<JSONObject>();
String jsonString =JsonToString(paramHashMap);
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject object=(JSONObject) jsonObj.get(objectName);
jsonList.add(object);
return jsonList;
}
}
------------------------------------gan-client-------------------------------------------------------
------------------------------------gan-server-------------------------------------------------------
/**
* JSON工具类,反射的方式转换整个对象
* @author Jim Wu
*
*/
public class JSONUtil{
private static JSONUtil instance = null;
public JSONUtil(){}
/**
* 代理类时做的检查.返回应该检查的对象.
* @param bean
* @return
*/
public static Object proxyCheck(Object bean){
return bean;
}
static public String toJSONString(Object obj) throws JSONException{
return toJSONString(obj, false);
}
static public String toJSONString(Object obj, boolean useClassConvert) throws JSONException{
if(instance == null)
instance = new JSONUtil();
return instance.getJSONObject(obj, useClassConvert).toString();
}
public static String getJSONArray(Object arrayObj, boolean useClassConvert) throws JSONException{
if(arrayObj == null)
return "null";
arrayObj = proxyCheck(arrayObj);
JSONArray jSONArray = new JSONArray();
if(arrayObj instanceof Collection){
Iterator iterator = ((Collection)arrayObj).iterator();
while(iterator.hasNext()){
Object rowObj = iterator.next();
if(rowObj == null)
jSONArray.put(new JSONStringObject(null));
else if(rowObj.getClass().isArray() || rowObj instanceof Collection)
jSONArray.put(getJSONArray(rowObj, useClassConvert));
else
jSONArray.put(getJSONObject(rowObj, useClassConvert));
}
}
if(arrayObj.getClass().isArray()){
int arrayLength = Array.getLength(arrayObj);
for(int i = 0; i < arrayLength; i ++){
Object rowObj = Array.get(arrayObj, i);
if(rowObj == null)
jSONArray.put(new JSONStringObject(null));
else if(rowObj.getClass().isArray() || rowObj instanceof Collection)
jSONArray.put(getJSONArray(rowObj, useClassConvert));
else
jSONArray.put(getJSONObject(rowObj, useClassConvert));
}
}
return jSONArray.toString();
}
//
public static JSONStringObject getJSONObject(Object value, boolean useClassConvert) throws JSONException{
//处理原始类型
if (value == null) {
return new JSONStringObject("null");
}
value = proxyCheck(value);
if (value instanceof JSONString) {
Object o;
try {
o = ((JSONString)value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
throw new JSONException("Bad value from toJSONString: " + o);
}
if (value instanceof Number){
return new JSONStringObject(JSONObject.numberToString((Number) value));
}
if (value instanceof Boolean || value instanceof JSONObject ||
value instanceof JSONArray) {
return new JSONStringObject(value.toString());
}
if (value instanceof String)
return new JSONStringObject(JSONObject.quote(value.toString()));
if (value instanceof Map) {
JSONObject jSONObject = new JSONObject();
Iterator iterator = ((Map)value).keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next().toString();
Object valueObj = ((Map)value).get(key);
jSONObject.put(key, getJSONObject(valueObj, useClassConvert));
}
return new JSONStringObject(jSONObject.toString());
}
//class
if(value instanceof Class)
return new JSONStringObject(JSONObject.quote(((Class)value).getSimpleName()));
//数组
if (value instanceof Collection || value.getClass().isArray()) {
return new JSONStringObject(getJSONArray(proxyCheck(value), useClassConvert));
}
return reflectObject(value, useClassConvert);
}//value.equals(null)
//
public static JSONStringObject reflectObject(Object bean, boolean useClassConvert) {
JSONObject jSONObject = new JSONObject();
Class klass = bean.getClass();
Method[] methods = klass.getMethods();
for (int i = 0; i < methods.length; i += 1) {
try {
Method method = methods[i];
String name = method.getName();
String key = "";
if (name.startsWith("get")) {
key = name.substring(3);
} else if (name.startsWith("is")) {
key = name.substring(2);
}
if (key.length() > 0 &&
Character.isUpperCase(key.charAt(0)) &&
method.getParameterTypes().length == 0) {
if (key.length() == 1) {
key = key.toLowerCase();
} else if (!Character.isUpperCase(key.charAt(1))) {
key = key.substring(0, 1).toLowerCase() +
key.substring(1);
}
Object elementObj = method.invoke(bean, null);
if(!useClassConvert && elementObj instanceof Class)
continue; //当if条件为true时,执行下面语句
jSONObject.put(key, getJSONObject(elementObj, useClassConvert));
}
} catch (Exception e) {
/* forget about it */
}
}
return new JSONStringObject(jSONObject.toString());
}
}
------------------------------------gan--------------------------------------------------------