Example usage for javax.json JsonObject getString

List of usage examples for javax.json JsonObject getString

Introduction

In this page you can find the example usage for javax.json JsonObject getString.

Prototype

String getString(String name);

Source Link

Document

A convenience method for getJsonString(name).getString()

Usage

From source file:Main.java

public static void main(String[] args) {
    String personJSONData = "  {" + "   \"name\": \"Jack\", " + "   \"age\" : 13, "
            + "   \"isMarried\" : false, " + "   \"address\": { " + "     \"street\": \"#1234, Main Street\", "
            + "     \"zipCode\": \"123456\" " + "   }, "
            + "   \"phoneNumbers\": [\"011-111-1111\", \"11-111-1111\"] " + " }";

    JsonReader reader = Json.createReader(new StringReader(personJSONData));

    JsonObject personObject = reader.readObject();

    reader.close();//from www.  j  a va  2  s  .  c o m

    System.out.println("Name   : " + personObject.getString("name"));
    System.out.println("Age    : " + personObject.getInt("age"));
    System.out.println("Married: " + personObject.getBoolean("isMarried"));

    JsonObject addressObject = personObject.getJsonObject("address");
    System.out.println("Address: ");
    System.out.println(addressObject.getString("street"));
    System.out.println(addressObject.getString("zipCode"));

    System.out.println("Phone  : ");
    JsonArray phoneNumbersArray = personObject.getJsonArray("phoneNumbers");
    for (JsonValue jsonValue : phoneNumbersArray) {
        System.out.println(jsonValue.toString());
    }
}

From source file:dk.dma.msinm.user.security.Credentials.java

/**
 * Attempts to parse the request JSON payload as Credentials, and returns null if it fails
 * @param requestBody the request/*from  ww w  .j a v a  2  s .  c o  m*/
 * @return the parsed credentials or null
 */
public static Credentials fromRequest(String requestBody) {
    Credentials credentials = new Credentials();
    try (JsonReader jsonReader = Json.createReader(new StringReader(requestBody))) {
        JsonObject jsonObject = jsonReader.readObject();
        credentials.setEmail(jsonObject.getString("email"));
        credentials.setPassword(jsonObject.getString("password"));
    } catch (Exception e) {
        // No valid credentials
    }
    return (credentials.isValid()) ? credentials : null;
}

From source file:org.jboss.set.aphrodite.stream.services.json.StreamComponentJsonParser.java

public static StreamComponent parse(JsonObject json) {
    try {//from   w w  w  .  j  a  v  a  2 s  . c  o  m
        final String name = json.getString(JSON_NAME);
        final List<String> contacts = getContacts(json);
        final RepositoryType repositoryType = RepositoryType.fromType(json.getString(JSON_REPOSITORY_TYPE));
        String repository = json.getString(JSON_REPOSITORY_URL);
        URI repositoryURI = null;
        if (repository != null) {
            // TODO: retain fix from chao for now, check if we need it at all ?
            if (!repository.endsWith("/")) {
                repository = repository + "/";
            }
            repositoryURI = new URI(repository);
        }
        final Codebase codeBase = new Codebase(json.getString(JSON_CODEBASE));
        final String tag = json.getString(JSON_TAG);
        final String version = json.getString(JSON_VERSION);
        final String gav = json.getString(JSON_GAV);
        final String comment = json.getString(JSON_COMMENT);
        return new StreamComponent(name, contacts, repositoryType, repositoryURI, codeBase, tag, version, gav,
                comment);
    } catch (Exception e) {
        Utils.logException(LOG, e);
        return null;
    }
}

From source file:io.hakbot.util.JsonUtil.java

/**
 * Returns the value for the specified parameter name included in the specified json object. If json
 * object is null or specified name cannot be found, method returns null.
 *//*w w w  .  j av  a  2s.c o  m*/
public static String getString(JsonObject json, String name) {
    return (json != null && json.containsKey(name)) ? json.getString(name) : null;
}

From source file:at.porscheinformatik.sonarqube.licensecheck.maven.MavenDependencyScanner.java

private static void parseDependencyJson(Set<Dependency> dependencies, JsonArray jsonDependencyArray) {
    for (int i = 0; i < jsonDependencyArray.size(); i++) {
        JsonObject jsonDependency = jsonDependencyArray.getJsonObject(i);
        String scope = jsonDependency.getString("s");
        if ("compile".equals(scope) || "runtime".equals(scope)) {
            if (jsonDependency.containsKey("d")) {
                parseDependencyJson(dependencies, jsonDependency.getJsonArray("d"));
            }/*from w  ww . j  a v a  2  s  .  co  m*/

            Dependency dependency = new Dependency(jsonDependency.getString("k"), jsonDependency.getString("v"),
                    null);
            dependencies.add(dependency);
        }
    }
}

From source file:org.fuin.esc.spi.Base64Data.java

/**
 * Creates in instance from the given JSON object.
 * //from ww w. j av a2  s . c o m
 * @param jsonObj
 *            Object to read values from.
 * 
 * @return New instance.
 */
public static Base64Data create(final JsonObject jsonObj) {
    final String base64Str = jsonObj.getString(EL_ROOT_NAME);
    return new Base64Data(base64Str);
}

From source file:at.porscheinformatik.sonarqube.licensecheck.mavenlicense.MavenLicense.java

public static List<MavenLicense> fromString(String mavenLicenseString) {
    List<MavenLicense> mavenLicenses = new ArrayList<>();

    if (mavenLicenseString != null && mavenLicenseString.startsWith("[")) {
        try (JsonReader jsonReader = Json.createReader(new StringReader(mavenLicenseString))) {
            JsonArray licensesJson = jsonReader.readArray();
            for (int i = 0; i < licensesJson.size(); i++) {
                JsonObject licenseJson = licensesJson.getJsonObject(i);
                mavenLicenses.add(new MavenLicense(licenseJson.getString("licenseNameRegEx"),
                        licenseJson.getString("license")));
            }/*from   w  w w  . ja va 2 s . c o  m*/
        }
    } else if (StringUtils.isNotEmpty(mavenLicenseString)) {
        // deprecated - remove with later release
        String[] mavenLicenseEntries = mavenLicenseString.split(";");
        for (String mavenLicenseEntry : mavenLicenseEntries) {
            String[] mavenLicenseEntryParts = mavenLicenseEntry.split("~");
            mavenLicenses.add(new MavenLicense(mavenLicenseEntryParts[0], mavenLicenseEntryParts[1]));
        }
    }

    return mavenLicenses;
}

From source file:com.lyonsdensoftware.config.DeviceConfigUtils.java

/**
 * Reads the {@link DeviceConfig} from disk. Pass in the name of the config file
 *
 * @return The configuration./* w w  w .  ja  v a 2 s. c  om*/
 */
public static DeviceConfig readConfigFile(String filename) {
    FileInputStream file = null;
    try {
        deviceConfigName = filename.trim();
        file = new FileInputStream(deviceConfigName);
        JsonReader json = Json.createReader(file);
        JsonObject configObject = json.readObject();

        JsonObject wundergroundObject = configObject.getJsonObject("wundergroundApi");
        String wundergroundApiKey = wundergroundObject.getString("wundergroundApiKey");
        String wundergroundStateIdentifier = wundergroundObject.getString("wundergroundStateIdentifier");
        String wundergroundCity = wundergroundObject.getString("wundergroundCity");

        String productId = configObject.getString("productId");
        String dsn = configObject.getString("dsn");

        JsonObject pythonTCPSettings = configObject.getJsonObject("pythonTCPSettings");
        String hostname = pythonTCPSettings.getString("hostname");
        int port = pythonTCPSettings.getInt("port");

        DeviceConfig deviceConfig = new DeviceConfig(wundergroundApiKey, wundergroundStateIdentifier,
                wundergroundCity, productId, dsn, hostname, port);

        return deviceConfig;
    } catch (FileNotFoundException e) {
        throw new RuntimeException("The required file " + deviceConfigName + " could not be opened.", e);
    } finally {
        IOUtils.closeQuietly(file);
    }
}

From source file:at.porscheinformatik.sonarqube.licensecheck.license.License.java

@Deprecated
private static void readLegacyJson(String serializedLicensesString, List<License> licenses) {
    // deprecated - remove with later release
    try (JsonReader jsonReader = Json.createReader(new StringReader(serializedLicensesString))) {
        JsonObject licensesJson = jsonReader.readObject();
        for (Map.Entry<String, JsonValue> licenseJson : licensesJson.entrySet()) {
            JsonObject value = (JsonObject) licenseJson.getValue();
            licenses.add(new License(value.getString("name"), licenseJson.getKey(), value.getString("status")));
        }//from   w  ww.  j  a va 2s. c o m
    }
}

From source file:at.porscheinformatik.sonarqube.licensecheck.license.License.java

public static List<License> fromString(String serializedLicensesString) {
    List<License> licenses = new ArrayList<>();

    if (serializedLicensesString != null) {
        if (serializedLicensesString.startsWith("[")) {
            try (JsonReader jsonReader = Json.createReader(new StringReader(serializedLicensesString))) {
                JsonArray licensesJson = jsonReader.readArray();
                for (JsonObject licenseJson : licensesJson.getValuesAs(JsonObject.class)) {
                    licenses.add(new License(licenseJson.getString("name"), licenseJson.getString("identifier"),
                            licenseJson.getString("status")));
                }//from   ww w.j av  a  2  s . c o m
            }
        } else if (serializedLicensesString.startsWith("{")) {
            readLegacyJson(serializedLicensesString, licenses);
        } else {
            readLegaySeparated(serializedLicensesString, licenses);
        }
    }
    return licenses;
}