Search in sources :

Example 21 with JSONArray

use of net.minidev.json.JSONArray in project molgenis by molgenis.

the class RestControllerV2IT method batchCreateLocation.

@Test
public void batchCreateLocation() {
    JSONObject jsonObject = new JSONObject();
    JSONArray entities = new JSONArray();
    JSONObject entity = new JSONObject();
    entity.put("Chromosome", "42");
    entity.put("Position", 42);
    entities.add(entity);
    jsonObject.put("entities", entities);
    String expectedLocation = "/api/v2/it_emx_datatypes_Locationv2?q=Position=in=(\"42\")";
    String expectedHref = "/api/v2/it_emx_datatypes_Locationv2/42";
    given().log().all().header(X_MOLGENIS_TOKEN, testUserToken).contentType(APPLICATION_JSON).body(jsonObject.toJSONString()).when().post(API_V2 + "it_emx_datatypes_Locationv2").then().statusCode(RestTestUtils.CREATED).log().all().body("location", Matchers.equalTo(expectedLocation), "resources[0].href", Matchers.equalTo(expectedHref));
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) Test(org.testng.annotations.Test)

Example 22 with JSONArray

use of net.minidev.json.JSONArray in project JsonPath by jayway.

the class Issue273 method testGetPropertyFromObject.

@Test
public void testGetPropertyFromObject() {
    String json = "{\n" + "    \"store\": {\n" + "        \"book\": [\n" + "            {\n" + "                \"category\": \"reference\",\n" + "                \"author\": \"Nigel Rees\",\n" + "                \"title\": \"Sayings of the Century\",\n" + "                \"price\": 8.95\n" + "            },\n" + "            {\n" + "                \"category\": \"fiction\",\n" + "                \"author\": \"Evelyn Waugh\",\n" + "                \"title\": \"Sword of Honour\",\n" + "                \"price\": 12.99\n" + "            },\n" + "            {\n" + "                \"category\": \"fiction\",\n" + "                \"author\": \"Herman Melville\",\n" + "                \"title\": \"Moby Dick\",\n" + "                \"isbn\": \"0-553-21311-3\",\n" + "                \"price\": 8.99\n" + "            },\n" + "            {\n" + "                \"category\": \"fiction\",\n" + "                \"author\": \"J. R. R. Tolkien\",\n" + "                \"title\": \"The Lord of the Rings\",\n" + "                \"isbn\": \"0-395-19395-8\",\n" + "                \"price\": 22.99\n" + "            }\n" + "        ],\n" + "        \"bicycle\": {\n" + "            \"color\": \"red\",\n" + "            \"price\": 19.95\n" + "        }\n" + "    },\n" + "    \"expensive\": 10\n" + "}\n" + "                ";
    JSONArray arr = JsonPath.read(json, "$..[2].author");
    assertEquals(arr.get(0), "Herman Melville");
}
Also used : JSONArray(net.minidev.json.JSONArray) Test(org.junit.Test)

Example 23 with JSONArray

use of net.minidev.json.JSONArray in project skype-bot by toomasr.

the class BitBucketHookHandler method handle.

@Override
public Map<String, String[]> handle(Map<String, String[]> parameters, String requestBody) {
    try {
        requestBody = URLDecoder.decode(requestBody, "UTF-8");
        requestBody = requestBody.replaceFirst("payload=", "");
        JSONObject object = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE).parse(requestBody);
        JSONObject repo = (JSONObject) object.get("repository");
        // String repoName = (String) repo.get("name");
        String absoluteUrl = (String) repo.get("absolute_url");
        String repoUrl = "https://bitbucket.org" + absoluteUrl;
        List<String> messages = new ArrayList<String>();
        JSONArray commits = (JSONArray) object.get("commits");
        for (int i = 0; i < commits.size(); i++) {
            JSONObject commit = (JSONObject) commits.get(i);
            String rawAuthor = (String) commit.get("raw_author");
            rawAuthor = rawAuthor.replaceFirst(" <.*>", "");
            String message = (String) commit.get("message");
            String branch = (String) commit.get("branch");
            messages.add(String.format("%s commited '%s' to %s (branch '%s')", rawAuthor, message, repoUrl, branch));
        }
        Map<String, String[]> replies = new HashMap<String, String[]>();
        replies.put(chatName, messages.toArray(new String[0]));
        return replies;
    } catch (Exception e) {
        throw new RuntimeException("Cannot parse json: " + requestBody, e);
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONParser(net.minidev.json.parser.JSONParser)

Example 24 with JSONArray

use of net.minidev.json.JSONArray in project json-android-compare by martinadamek.

the class SmartJson method parsePublicTimeline.

public List<Map> parsePublicTimeline(InputStream inputStream) {
    List<Map> result = new ArrayList<Map>();
    JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
    try {
        Map map;
        Set keys;
        Set keys2;
        JSONObject user;
        JSONObject jsonObject;
        JSONArray jsonArray = (JSONArray) p.parse(new InputStreamReader(inputStream));
        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            map = new HashMap();
            jsonObject = (JSONObject) jsonArray.get(i);
            keys = jsonObject.keySet();
            for (Object key : keys) {
                if ("user".equals(key)) {
                    user = (JSONObject) jsonObject.get(key);
                    keys2 = user.keySet();
                    for (Object key2 : keys2) {
                        map.put("user." + key2, user.get(key2));
                    }
                } else {
                    map.put(key, jsonObject.get(key));
                }
            }
            result.add(map);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : Set(java.util.Set) JSONObject(net.minidev.json.JSONObject) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with JSONArray

use of net.minidev.json.JSONArray in project spring-boot by spring-projects.

the class BootCuriesHrefIntegrationTests method getCurieHref.

private String getCurieHref(String uri) {
    ResponseEntity<String> response = new TestRestTemplate().getForEntity(uri, String.class);
    JSONArray bootCuriesHrefs = JsonPath.parse(response.getBody()).read("_links.curies[?(@.name == 'boot')].href");
    assertThat(bootCuriesHrefs).hasSize(1);
    return (String) bootCuriesHrefs.get(0);
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) JSONArray(net.minidev.json.JSONArray)

Aggregations

JSONArray (net.minidev.json.JSONArray)55 JSONObject (net.minidev.json.JSONObject)41 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 JSONParser (net.minidev.json.parser.JSONParser)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONConverterException (org.btrplace.json.JSONConverterException)3 VM (org.btrplace.model.VM)3 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 ContentType (ddf.catalog.data.ContentType)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)2 SourceDescriptor (ddf.catalog.source.SourceDescriptor)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)2