Example usage for javax.json JsonReader readObject

List of usage examples for javax.json JsonReader readObject

Introduction

In this page you can find the example usage for javax.json JsonReader readObject.

Prototype

JsonObject readObject();

Source Link

Document

Returns a JSON object that is represented in the input source.

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   w  w w.j av a 2s  .  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:httputils.HttpUtil.java

private static JsonObject convertSomethingToJson(InputStream is) {
    JsonReader reader = Json.createReader(is);
    JsonObject jObj = reader.readObject();
    reader.close();//from   ww w.  java 2s  . com

    return jObj;
}

From source file:httputils.HttpUtil.java

public static JsonObject convertResponseToJson(HttpResponse response) throws ParseException, IOException {
    InputStream in = response.getEntity().getContent();
    JsonReader reader = Json.createReader(in);
    JsonObject jObj = reader.readObject();
    reader.close();//from w ww.j  a  va 2 s  .co m
    in.close();

    return jObj;
}

From source file:httputils.HttpUtil.java

public static JsonObject convertStringToJson(String jstring) {
    StringReader in = new StringReader(jstring);
    JsonReader reader = Json.createReader(in);
    JsonObject jObj = reader.readObject();
    reader.close();/*from ww w. j a  v a2s  .  c  o m*/
    in.close();

    return jObj;
}

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

/**
 * Creates a JsonObject (a Map implementation) from a json-formatted string
 *///w ww.j a  v  a  2 s .  c o  m
public static JsonObject toJsonObject(String jsonString) {
    JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
    return jsonReader.readObject();
}

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.//from w  ww .j  a va2  s .  co m
 */
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:io.hakbot.util.JsonUtil.java

/**
 * Creates a JsonObject (a Map implementation) from a json-formatted byte[] array
 *//*from  w ww .j av  a2 s.c  o m*/
public static JsonObject toJsonObject(byte[] jsonBytes) {
    JsonReader jsonReader = Json.createReader(new StringReader(new String(jsonBytes)));
    return jsonReader.readObject();
}

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

public static Map<String, Stream> parse(final URL url) throws NotFoundException {
    try (InputStream is = url.openStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        JsonReader jr = Json.createReader(rd);

        final JsonArray jsonArray = jr.readObject().getJsonArray(JSON_STREAMS);
        Objects.requireNonNull(jsonArray, "streams array must be specified in json file");
        final Map<String, Stream> streamMap = new LinkedHashMap<String, Stream>();
        for (JsonValue value : jsonArray) {
            JsonObject json = (JsonObject) value;

            String upstreamName = json.getString(JSON_UPSTREAM, null);
            Stream upstream = streamMap.get(upstreamName);

            JsonArray codebases = json.getJsonArray(JSON_CODEBASES);
            Map<String, StreamComponent> codebaseMap = parseStreamCodebases(codebases);

            Stream currentStream = new Stream(url, json.getString(JSON_NAME), upstream, codebaseMap);
            streamMap.put(currentStream.getName(), currentStream);
        }//from w  ww . j ava 2 s. c o m
        return streamMap;
    } catch (Exception e) {
        Utils.logException(LOG, "Unable to load url: " + url.toString(), e);
        throw new NotFoundException(e);
    }
}

From source file:org.json.StackExchangeAPI.java

private static void parseStackExchange(String jsonStr) {
    JsonReader reader = null;
    try {/*from   w  w  w  .j  ava  2s  . c  o  m*/
        reader = Json.createReader(new StringReader(jsonStr));
        JsonObject jsonObject = reader.readObject();
        reader.close();
        JsonArray array = jsonObject.getJsonArray("items");
        for (JsonObject result : array.getValuesAs(JsonObject.class)) {

            JsonObject ownerObject = result.getJsonObject("owner");
            // int ownerReputation = ownerObject.getInt("reputation");
            //  System.out.println("Reputation:"+ownerReputation);
            int viewCount = result.getInt("view_count");
            System.out.println("View Count :" + viewCount);
            int answerCount = result.getInt("answer_count");
            System.out.println("Answer Count :" + answerCount);
            String link = result.getString("link");
            System.out.println("URL: " + link);
            String title = result.getString("title");
            System.out.println("Title: " + title);
            String body = result.getString("body");
            System.out.println("Body: " + body);
            JsonArray tagsArray = result.getJsonArray("tags");
            StringBuilder tagBuilder = new StringBuilder();
            int i = 1;
            for (JsonValue tag : tagsArray) {
                tagBuilder.append(tag.toString());
                if (i < tagsArray.size())
                    tagBuilder.append(",");
                i++;
            }

            System.out.println("Tags: " + tagBuilder.toString());
            System.out.println("------------------------------------------");
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.amazon.alexa.avs.config.DeviceConfigUtils.java

/**
 * Reads the {@link DeviceConfig} from disk. Pass in the name of the config file
 *
 * @return The configuration.//ww  w. ja  v a2s .c o  m
 */
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 companionServiceObject = configObject.getJsonObject(DeviceConfig.COMPANION_SERVICE);
        CompanionServiceInformation companionServiceInfo = null;
        if (companionServiceObject != null) {
            String serviceUrl = companionServiceObject
                    .getString(DeviceConfig.CompanionServiceInformation.SERVICE_URL, null);
            String sessionId = companionServiceObject
                    .getString(DeviceConfig.CompanionServiceInformation.SESSION_ID, null);
            String sslClientKeyStore = companionServiceObject
                    .getString(DeviceConfig.CompanionServiceInformation.SSL_CLIENT_KEYSTORE, null);
            String sslClientKeyStorePassphrase = companionServiceObject
                    .getString(DeviceConfig.CompanionServiceInformation.SSL_CLIENT_KEYSTORE_PASSPHRASE, null);
            String sslCaCert = companionServiceObject
                    .getString(DeviceConfig.CompanionServiceInformation.SSL_CA_CERT, null);

            companionServiceInfo = new CompanionServiceInformation(serviceUrl, sslClientKeyStore,
                    sslClientKeyStorePassphrase, sslCaCert);
            companionServiceInfo.setSessionId(sessionId);
        }

        JsonObject companionAppObject = configObject.getJsonObject(DeviceConfig.COMPANION_APP);
        CompanionAppInformation companionAppInfo = null;
        if (companionAppObject != null) {
            int localPort = companionAppObject.getInt(DeviceConfig.CompanionAppInformation.LOCAL_PORT, -1);
            String lwaUrl = companionAppObject.getString(DeviceConfig.CompanionAppInformation.LWA_URL, null);
            String clientId = companionAppObject.getString(DeviceConfig.CompanionAppInformation.CLIENT_ID,
                    null);
            String refreshToken = companionAppObject
                    .getString(DeviceConfig.CompanionAppInformation.REFRESH_TOKEN, null);
            String sslKeyStore = companionAppObject.getString(DeviceConfig.CompanionAppInformation.SSL_KEYSTORE,
                    null);
            String sslKeyStorePassphrase = companionAppObject
                    .getString(DeviceConfig.CompanionAppInformation.SSL_KEYSTORE_PASSPHRASE, null);

            companionAppInfo = new CompanionAppInformation(localPort, lwaUrl, sslKeyStore,
                    sslKeyStorePassphrase);
            companionAppInfo.setClientId(clientId);
            companionAppInfo.setRefreshToken(refreshToken);
        }

        String productId = configObject.getString(DeviceConfig.PRODUCT_ID, null);
        String dsn = configObject.getString(DeviceConfig.DSN, null);
        String provisioningMethod = configObject.getString(DeviceConfig.PROVISIONING_METHOD, null);
        String avsHost = configObject.getString(DeviceConfig.AVS_HOST, null);
        boolean wakeWordAgentEnabled = configObject.getBoolean(DeviceConfig.WAKE_WORD_AGENT_ENABLED, false);
        String locale = configObject.getString(DeviceConfig.LOCALE, null);

        DeviceConfig deviceConfig = new DeviceConfig(productId, dsn, provisioningMethod, wakeWordAgentEnabled,
                locale, companionAppInfo, companionServiceInfo, avsHost);

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