Code example for JSONArray

Methods: put, toString

0
     
    @Test 
    public void testEchoWithJSONRPC20BindingBatch() throws Exception { 
        JSONObject jsonRequest1 = new JSONObject("{ \"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"Hello JSON-RPC\"], \"id\": 1}");
        JSONObject jsonRequest2 = new JSONObject("{ \"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"Hello JSON-RPC 2.0\"], \"id\": 2}");
        JSONArray batchReq = new JSONArray();
        batchReq.put(jsonRequest1);
        batchReq.put(jsonRequest2);
         
        WebConversation wc = new WebConversation();
        WebRequest request   = new PostMethodWebRequest( SERVICE20_URL, new ByteArrayInputStream(batchReq.toString().getBytes("UTF-8")),"application/json");
        WebResponse response = wc.getResource(request);
 
        Assert.assertEquals(200, response.getResponseCode());
 
        JSONArray jsonResp = new JSONArray(response.getText());
        Assert.assertEquals("echo: Hello JSON-RPC", ((JSONObject) jsonResp.get(0)).getString("result"));
        Assert.assertEquals("echo: Hello JSON-RPC 2.0", ((JSONObject) jsonResp.get(1)).getString("result"));
    } 
 
    @Test