本文整理匯總了Java中net.sf.json.JSONObject.accumulate方法的典型用法代碼示例。如果您正苦於以下問題:Java JSONObject.accumulate方法的具體用法?Java JSONObject.accumulate怎麽用?Java JSONObject.accumulate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.sf.json.JSONObject
的用法示例。
在下文中一共展示了JSONObject.accumulate方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doPost
import net.sf.json.JSONObject; //導入方法依賴的package包/類
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JSONObject voo = new JSONObject();
voo.put("aircraft", "A320");
voo.put("maxPax", 200);
JSONObject piloto = new JSONObject();
piloto.put("firstName", "John");
piloto.put("lastName", "Doe");
voo.put("pilot", piloto);
voo.accumulate("passenger", "George");
voo.accumulate("passenger", "Thomas");
// enviar os dados no formato JSON
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
out.println(voo.toString(2));
}
示例2: map2jsonConverter
import net.sf.json.JSONObject; //導入方法依賴的package包/類
/**
* Converts a Map Object into JSON string.
* @param hm
* @return
*/
public static String map2jsonConverter(Map hm) {
if (hm==null || hm.size()==0) {
return "{}";
}
JSONObject jo = new JSONObject();
for (Iterator it = hm.keySet().iterator(); it.hasNext();) {
Object k = it.next();
if (hm.get(k)==null || hm.get(k) instanceof String) {
jo.accumulate(k.toString(), hm.get(k));
} else if (hm.get(k) instanceof Collection || hm.get(k).getClass().isArray()) {
jo.accumulate(k.toString(), JSONArray.fromObject(hm.get(k)).toString());
} else {
log.debug("map2jsonConverter: "+hm.get(k).getClass().getName());
jo.accumulate(k.toString(), JSONObject.fromObject(hm.get(k)).toString());
}
}
return jo.toString();
}
示例3: requestParams2json
import net.sf.json.JSONObject; //導入方法依賴的package包/類
/**
* Convert the request parameter map to a json string.
* @param request
* @return
*/
public static String requestParams2json(ServletRequest request) {
Map<String, String[]> params = request.getParameterMap();
JSONObject jo = new JSONObject();
for (Iterator<String> it=params.keySet().iterator(); it.hasNext();) {
String k = it.next();
String[] v = params.get(k);
if (v==null || v.length==0) {
continue;
} else if (v.length==1) {
jo.accumulate(k, v[0]);
} else {
jo.accumulate(k, JSONArray.fromObject(v));
}
}
return jo.toString();
}