Search in sources :

Example 51 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class ExportTest method test_json.

@Test
public void test_json() throws IOException {
    String xJson = Export.toJson(new X());
    JSONObject jsonObj = JSONObject.fromObject(xJson);
    Assert.assertEquals(X.class.getName(), jsonObj.getString("_class"));
    Assert.assertEquals("xVal", jsonObj.getString("val"));
}
Also used : JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 52 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class SSEConnection method subscribe.

public void subscribe(String channel) throws IOException {
    // constructor is not public
    // EventFilter f = new EventFilter();
    JSONObject o = new JSONObject();
    o.put("jenkins_channel", channel);
    EventFilter f = (EventFilter) o.toBean(EventFilter.class);
    configure(Collections.singletonList(f), Collections.<EventFilter>emptyList());
}
Also used : JSONObject(net.sf.json.JSONObject) EventFilter(org.jenkinsci.plugins.pubsub.EventFilter)

Example 53 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class SSEConnection method toJSONArray.

private JSONArray toJSONArray(Collection<EventFilter> filters) {
    JSONArray a = new JSONArray();
    for (EventFilter f : filters) {
        JSONObject o = new JSONObject();
        o.putAll(f);
        a.add(o);
    }
    return a;
}
Also used : JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray) EventFilter(org.jenkinsci.plugins.pubsub.EventFilter)

Example 54 with JSONObject

use of net.sf.json.JSONObject in project pmph by BCSquad.

the class HttpRequestUtil method httpRequest.

/**
 * 发起https请求并获取结果
 *
 * @param requestUrl 请求地址
 * @param requestMethod 请求方式(GET、POST)
 * @param outputStr 提交的数据
 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
 */
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
    System.err.println(requestMethod + "\toutputStr=" + outputStr);
    JSONObject jsonObject = null;
    StringBuffer buffer = new StringBuffer();
    try {
        // 创建SSLContext对象,并使用我们指定的信任管理器初始化
        TrustManager[] tm = { new MyX509TrustManager() };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        // 从上述SSLContext对象中得到SSLSocketFactory对象
        SSLSocketFactory ssf = sslContext.getSocketFactory();
        URL url = new URL(requestUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
        httpUrlConn.setSSLSocketFactory(ssf);
        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);
        // 设置请求方式(GET/POST)
        httpUrlConn.setRequestMethod(requestMethod);
        if ("GET".equalsIgnoreCase(requestMethod))
            httpUrlConn.connect();
        // 当有数据需要提交时
        if (null != outputStr) {
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式,防止中文乱码
            outputStream.write(outputStr.getBytes("UTF-8"));
            outputStream.close();
        }
        // 将返回的输入流转换成字符串
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        // 释放资源
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();
        jsonObject = JSONObject.fromObject(buffer.toString());
    // System.out.println("jsonObject="+jsonObject);
    } catch (ConnectException ce) {
        logger.error("网络链接:{}", ce.getMessage());
    } catch (UnknownHostException uhe) {
        logger.error("微信API访问异常:{}", uhe.getMessage());
    // httpRequest(requestUrl, requestMethod, outputStr);
    } catch (Exception e) {
        logger.error("出现异常:{}", e.getMessage());
    }
    return jsonObject;
}
Also used : InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) UnknownHostException(java.net.UnknownHostException) ConnectException(java.net.ConnectException) TrustManager(javax.net.ssl.TrustManager) SecureRandom(java.security.SecureRandom) JSONObject(net.sf.json.JSONObject) BufferedReader(java.io.BufferedReader) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) ConnectException(java.net.ConnectException)

Example 55 with JSONObject

use of net.sf.json.JSONObject in project pmph by BCSquad.

the class TestSendMes method Send_msg.

/**
 * <pre>
 * 功能描述:主动发送文字给企业用户
 * 使用示范:
 *
 * @param touser 成员ID列表
 * @param toparty 部门ID列表
 * @param totag 标签ID列表
 * @param msgtype 消息类型,此时固定为:text (支持消息型应用跟主页型应用)
 * @param agentid 企业应用的id,整型。可在应用的设置页面查看
 * @param content 消息内容,最长不超过2048个字节,注意:主页型应用推送的文本消息在微信端最多只显示20个字(包含中英文)
 * @return int 表示是否是保密消息,0表示否,1表示是,默认0
 * </pre>
 */
public static int Send_msg(String touser, String toparty, String totag, String msgtype, int agentid, String content) {
    int errCode = 0;
    // 拼接请求地址
    String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", WechatAccessToken.getAccessToken(Constants.CORPID, Constants.SECRET, 1).getToken());
    // 需要提交的数据
    // String postJson =
    // "{\"agentid\":\"%s\",\"touser\":\"%s\",\"toparty\":\"[1,%s]\",\"totag\":\"%s\",\"msgtype\":\"text\",\"%s\":{\"content\":\"%s\"},\"safe\":\"0\"}";
    // String postJson =
    // "{\"agentid\":\"%s\",\"touser\":\"%s\",\"toparty\":\"%s\",\"totag\":\"%s\",\"msgtype\":\"text\",\"%s\":{\"content\":\"%s\"},\"safe\":\"0\"}";
    String postJson = "{\"agentid\":%s,\"touser\": \"%s\",\"toparty\": \"%s\",\"totag\": \"%s\"," + "\"msgtype\":\"%s\",\"text\": {\"content\": \"%s\"},\"safe\":0}";
    String outputStr = String.format(postJson, agentid, touser, toparty, totag, msgtype, content);
    // System.out.println(outputStr);
    // 创建成员
    JSONObject jsonObject = HttpRequestUtil.httpRequest(requestUrl, "POST", outputStr);
    if (null != jsonObject) {
        // System.out.println(jsonObject.toString() + "=====");
        // int errcode = jsonObject.getInt("errcode");
        errCode = jsonObject.getInt("errcode");
    }
    return errCode;
}
Also used : JSONObject(net.sf.json.JSONObject)

Aggregations

JSONObject (net.sf.json.JSONObject)493 Test (org.junit.Test)99 JSONArray (net.sf.json.JSONArray)94 IOException (java.io.IOException)49 HashMap (java.util.HashMap)48 ArrayList (java.util.ArrayList)36 JSON (net.sf.json.JSON)26 PrintWriter (java.io.PrintWriter)25 Map (java.util.Map)23 File (java.io.File)21 InputStream (java.io.InputStream)18 URISyntaxException (java.net.URISyntaxException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 JsonConfig (net.sf.json.JsonConfig)14 FreeStyleBuild (hudson.model.FreeStyleBuild)13 URI (java.net.URI)13 URL (java.net.URL)13 JSONException (net.sf.json.JSONException)13 Context (org.zaproxy.zap.model.Context)12 Transactional (org.springframework.transaction.annotation.Transactional)11