本文整理汇总了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();
}