public static Document fromJson(String jsonText) { final JsonParser parser = new JsonParser(); final JsonObject json = parser.parse(jsonText).getAsJsonObject(); if (!json.isJsonObject()) { throw new IllegalArgumentException("expected object as JSON root"); } final Document doc = (Document) JsonUtils.toEntry(json); if (doc == null) { throw new IllegalStateException(); } return doc; }
String companyTitle = ""; JsonParser parser = new JsonParser(); JsonElement element = parser.parse(jsonResponseString); JsonObject object = element.getAsJsonObject(); if (object.isJsonObject()) { JsonArray companies = object.getAsJsonArray("d"); JsonObject company = companies.get(0).getAsJsonObject(); companyTitle = company .get("Company").getAsString(); }
/** * Unmarshall. * * @param apiUrl * the api url * * @return the feed */ protected Feed unmarshall(String apiUrl) { JsonObject response = unmarshall(callApiGet(apiUrl)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GitHubException(json.get("responseDetails").getAsString()); } JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(new TypeToken<Feed>(){}, data.getAsJsonObject().get("feed")); } } return null; }
private int requestNewProxyPort(String upstreamHttpProxy) { try { // Request BMP to create a new Proxy HttpPost request = new HttpPost(requestURIBuilder() .setPath("/proxy") .build()); // Add form parameters to the request applyFormParamsToHttpRequest(request, new BasicNameValuePair("httpProxy", upstreamHttpProxy)); // Execute request CloseableHttpResponse response = HTTPclient.execute(request); // Parse response into JSON JsonObject createProxyResponseJson = httpResponseToJsonObject(response); response.close(); if (null == createProxyResponseJson || !createProxyResponseJson.isJsonObject()) { throw new RuntimeException("Unexpected Response JSON: " + createProxyResponseJson); } return createProxyResponseJson.getAsJsonPrimitive("port").getAsInt(); } catch (Exception e) { throw new BMPCUnableToConnectException(String.format( "Unable to connect to BMP Proxy at '%s:%s'", APIHost, APIPort ), e); } }
@Test public void shouldTransformEmptyRoot() throws IOException { JsonConfigCollection empty = readRootYaml("empty"); assertThat(empty.getJsonObject().isJsonObject(), is(true)); assertThat(empty.getJsonObject().get("environments").isJsonArray(), is(true)); assertThat(empty.getJsonObject().get("pipelines").isJsonArray(), is(true)); assertThat(empty.getJsonObject().get("environments").getAsJsonArray().size(), is(0)); assertThat(empty.getJsonObject().get("pipelines").getAsJsonArray().size(), is(0)); }