本文整理汇总了Java中net.sf.json.JSONObject.toString方法的典型用法代码示例。如果您正苦于以下问题:Java JSONObject.toString方法的具体用法?Java JSONObject.toString怎么用?Java JSONObject.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.json.JSONObject
的用法示例。
在下文中一共展示了JSONObject.toString方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: responseResult
import net.sf.json.JSONObject; //导入方法依赖的package包/类
/**
* 返回服务端处理结果
* @param obj 服务端输出对象
* @return 输出处理结果给前段JSON格式数据
* @author YANGHONGXIA
* @since 2015-01-06
*/
public String responseResult(Object obj){
JSONObject jsonObj = null;
if(obj != null){
logger.info("后端返回对象:{}", obj);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
jsonObj = JSONObject.fromObject(obj, jsonConfig);
logger.info("后端返回数据:" + jsonObj);
if(HttpConstants.SERVICE_RESPONSE_SUCCESS_CODE.equals(jsonObj.getString(HttpConstants.SERVICE_RESPONSE_RESULT_FLAG))){
jsonObj.element(HttpConstants.RESPONSE_RESULT_FLAG_ISERROR, false);
jsonObj.element(HttpConstants.SERVICE_RESPONSE_RESULT_MSG, "");
}else{
jsonObj.element(HttpConstants.RESPONSE_RESULT_FLAG_ISERROR, true);
String errMsg = jsonObj.getString(HttpConstants.SERVICE_RESPONSE_RESULT_MSG);
jsonObj.element(HttpConstants.SERVICE_RESPONSE_RESULT_MSG, errMsg==null?HttpConstants.SERVICE_RESPONSE_NULL:errMsg);
}
}
logger.info("输出结果:{}", jsonObj.toString());
return jsonObj.toString();
}
示例2: getOption
import net.sf.json.JSONObject; //导入方法依赖的package包/类
/**
* replacement的value为String类型时,直接根据key替换,否则转为Json》String后再替换
* @author ack (chenzhao) @DATE: Aug 23, 2017
* @param replacement
* @return 返回生成option字符串
*/
public String getOption(LinkedHashMap<String, Object> replacement){
Set<Entry<String, Object>> entrySet=replacement.entrySet();
Iterator<Entry<String, Object>> iterator=entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, Object>entry=(Entry<String, Object>) iterator.next();
//如果value非String,则将value转为json再转为String
String value=entry.getValue()+"";
Object objValue=entry.getValue() ;
if(!(objValue instanceof String)){
if(objValue instanceof List||objValue instanceof Object[]){//如果是数组或集合
JSONArray jsonArray=JSONArray.fromObject(objValue);
value=jsonArray.toString();
}else{
JSONObject jsonObjecct=JSONObject.fromObject(entry.getValue());
value=jsonObjecct.toString();
}
}
//根据key替换Option字符串里的字符
chartString=chartString.replace(entry.getKey()+"", value);
}
return chartString;
}
示例3: 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();
}
示例4: 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();
}
示例5: test_0
import net.sf.json.JSONObject; //导入方法依赖的package包/类
public void test_0() throws Exception {
JSONObject obj = new JSONObject();
obj.put("a", new A());
String text = obj.toString();
System.out.println(text);
}
示例6: sendData
import net.sf.json.JSONObject; //导入方法依赖的package包/类
/**
* Send test json data for the report
* @return session in JSONObject
*/
public JSONObject sendData(JSONObject data) throws IOException {
String uri = httpUtils.getDataAddress() +
String.format("/submit.php?session_id=%s&signature=%s&test_id=%s&user_id=%s",
getId(), signature, testId, userId);
uri += "&pq=0&target=labels_bulk&update=1"; //TODO: % self.kpi_target
String dataStr = data.toString();
log.debug("Sending active test data: " + dataStr);
JSONObject response = httpUtils.queryObject(httpUtils.createPost(uri, dataStr), 200);
return response.getJSONObject("result").getJSONObject("session");
}
示例7: getJsonList
import net.sf.json.JSONObject; //导入方法依赖的package包/类
@Transactional
public String getJsonList(PageBean pageBean, Map<String, Object> map,
String order, HttpServletRequest request,
Map<String, Object> otherParams, String[] excludes) {
if (order == null && request != null) {
order = " ";
order += StringUtils.isEmpty(request.getParameter("sortname")) ? " id"
: " " + request.getParameter("sortname");
order += StringUtils.isEmpty(request.getParameter("sortorder")) ? " desc"
: " " + request.getParameter("sortorder");
}
List<T> list = new ArrayList<T>();
list = getList(pageBean, map, order);
HashMap<String, Object> tempMap = new HashMap<String, Object>();
tempMap.put(Const.Page, pageBean);
tempMap.put(Const.Rows, list);
tempMap.put("Total", pageBean.getDataSize());
if (otherParams != null) {
Iterator<Entry<String, Object>> iter = otherParams.entrySet()
.iterator();
while (iter.hasNext()) {
Entry<String, Object> entry = iter.next();
tempMap.put(entry.getKey(), entry.getValue());
}
}
JsonConfig config = new JsonConfig();
if (excludes == null) {
excludes = new String[] {}; // 待添�?
}
config.setExcludes(excludes);// 出去dept属�?
JSONObject json = JSONObject.fromObject(tempMap, config);
return json.toString();
}
示例8: responseSuccess
import net.sf.json.JSONObject; //导入方法依赖的package包/类
/**
* 返回成功
* @param obj 输出对象
* @return 输出成功的JSON格式数据
*/
public String responseSuccess(Object obj){
JSONObject jsonObj = null;
if(obj != null){
logger.info("后端返回对象:{}", obj);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
jsonObj = JSONObject.fromObject(obj, jsonConfig);
logger.info("后端返回数据:" + jsonObj);
jsonObj.element(HttpConstants.RESPONSE_RESULT_FLAG_ISERROR, false);
jsonObj.element(HttpConstants.SERVICE_RESPONSE_RESULT_MSG, "");
}
logger.info("输出结果:{}", jsonObj.toString());
return jsonObj.toString();
}
示例9: responseMemberSuccess
import net.sf.json.JSONObject; //导入方法依赖的package包/类
/**
* 返回成功
* @param obj 输出对象
* @return 输出成功的JSON格式数据
*/
public String responseMemberSuccess(Object obj){
JSONObject jsonObj = null;
if(obj != null){
logger.info("后端返回对象:{}", obj);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
jsonObj = JSONObject.fromObject(obj, jsonConfig);
logger.info("后端返回数据:" + jsonObj);
}
logger.info("输出结果:{}", jsonObj.toString());
return jsonObj.toString();
}
示例10: responseFail
import net.sf.json.JSONObject; //导入方法依赖的package包/类
/**
* 返回失败
* @param errorMsg 错误信息
* @return 输出失败的JSON格式数据
*/
public String responseFail(String errorMsg){
JSONObject jsonObj = new JSONObject();
jsonObj.put(HttpConstants.RESPONSE_RESULT_FLAG_ISERROR, true);
jsonObj.put(HttpConstants.SERVICE_RESPONSE_RESULT_MSG, errorMsg);
logger.info("输出结果:{}", jsonObj.toString());
return jsonObj.toString();
}
示例11: postToWebhook
import net.sf.json.JSONObject; //导入方法依赖的package包/类
public static void postToWebhook(String webhook, boolean deployableMessage, JSONObject message, PrintStream printStream){
//check webhook
if(Util.isNullOrEmpty(webhook)){
printStream.println("[IBM Cloud DevOps] IBM_CLOUD_DEVOPS_WEBHOOK_URL not set.");
printStream.println("[IBM Cloud DevOps] Error: Failed to notify OTC.");
} else {
// set a 5 seconds timeout
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(defaultRequestConfig)
.build();
HttpPost postMethod = new HttpPost(webhook);
try {
StringEntity data = new StringEntity(message.toString());
postMethod.setEntity(data);
postMethod = Proxy.addProxyInformation(postMethod);
postMethod.addHeader("Content-Type", "application/json");
if (deployableMessage) {
postMethod.addHeader("x-create-connection", "true");
printStream.println("[IBM Cloud DevOps] Sending Deployable Mapping message to webhook:");
printStream.println(message);
}
CloseableHttpResponse response = httpClient.execute(postMethod);
if (response.getStatusLine().toString().matches(".*2([0-9]{2}).*")) {
printStream.println("[IBM Cloud DevOps] Message successfully posted to webhook.");
} else {
printStream.println("[IBM Cloud DevOps] Message failed, response status: " + response.getStatusLine());
}
} catch (IOException e) {
printStream.println("[IBM Cloud DevOps] IOException, could not post to webhook:");
e.printStackTrace(printStream);
}
}
}
示例12: generateJSONResponse
import net.sf.json.JSONObject; //导入方法依赖的package包/类
private String generateJSONResponse(Result result) {
JSONObject out = new JSONObject();
out.put("input", result.getText());
JSONObject sent = new JSONObject();
sent.put("label", result.getSentiment());
sent.put("score", result.getSentimentScore());
out.put("sentiment", sent);
JSONObject rel = new JSONObject();
rel.put("label", result.getRelevance());
rel.put("score", result.getRelevanceScore());
out.put("relevance", rel);
JSONObject asp = new JSONObject();
asp.put("label", result.getAspect());
asp.put("score", result.getAspectScore());
out.put("aspect", asp);
JSONObject aspCoarse = new JSONObject();
aspCoarse.put("label", result.getAspectCoarse());
aspCoarse.put("score", result.getAspectCoarseScore());
out.put("aspect_coarse", aspCoarse);
JSONArray targets = new JSONArray();
for (AspectExpression a : result.getAspectExpressions()) {
targets.add(a.getAspectExpression());
}
out.put("targets", targets);
return out.toString();
}