Example usage for javax.json JsonObject getInt

List of usage examples for javax.json JsonObject getInt

Introduction

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

Prototype

int getInt(String name);

Source Link

Document

A convenience method for getJsonNumber(name).intValue()

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();// ww w.  j  ava 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: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 -1.
 */// www . j a  va  2 s  .  c  om
public static int getInt(JsonObject json, String name) {
    return (json != null && json.containsKey(name)) ? json.getInt(name) : -1;
}

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  av a2  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:org.json.StackExchangeAPI.java

private static void parseStackExchange(String jsonStr) {
    JsonReader reader = null;/*from   w ww  . jav a  2s .co m*/
    try {
        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:io.bitgrillr.gocddockerexecplugin.utils.GoTestUtils.java

/**
 * Schedules the named pipeline to run./*from w  w w  . j av  a 2s.  com*/
 *
 * @param pipeline Name of the Pipeline to run.
 * @return Counter of the pipeline instance scheduled.
 * @throws GoError If Go.CD returns a non 2XX response.
 * @throws IOException If a communication error occurs.
 * @throws InterruptedException If something has gone horribly wrong.
 */
public static int runPipeline(String pipeline) throws GoError, IOException, InterruptedException {
    final Response scheduleResponse = executor
            .execute(Request.Post(PIPELINES + pipeline + "/schedule").addHeader("Confirm", "true"));
    final int scheduleStatus = scheduleResponse.returnResponse().getStatusLine().getStatusCode();
    if (scheduleStatus != HttpStatus.SC_ACCEPTED) {
        throw new GoError(scheduleStatus);
    }

    Thread.sleep(5 * 1000);

    final HttpResponse historyResponse = executor.execute(Request.Get(PIPELINES + pipeline + "/history"))
            .returnResponse();
    final int historyStatus = historyResponse.getStatusLine().getStatusCode();
    if (historyStatus != HttpStatus.SC_OK) {
        throw new GoError(historyStatus);
    }
    final JsonArray pipelineInstances = Json.createReader(historyResponse.getEntity().getContent()).readObject()
            .getJsonArray("pipelines");
    JsonObject lastPipelineInstance = pipelineInstances.getJsonObject(0);
    for (JsonValue pipelineInstance : pipelineInstances) {
        if (pipelineInstance.asJsonObject().getInt("counter") > lastPipelineInstance.getInt("counter")) {
            lastPipelineInstance = pipelineInstance.asJsonObject();
        }
    }

    return lastPipelineInstance.getInt("counter");
}

From source file:org.apache.tamaya.etcd.EtcdAccessor.java

private static void parsePrevNode(String key, Map<String, String> result, JsonObject o) {
    if (o.containsKey("prevNode")) {
        final JsonObject prevNode = o.getJsonObject("prevNode");
        if (prevNode.containsKey("createdIndex")) {
            result.put("_" + key + ".prevNode.createdIndex", String.valueOf(prevNode.getInt("createdIndex")));
        }/*from   w  w w .  j  ava2 s  .co m*/
        if (prevNode.containsKey("modifiedIndex")) {
            result.put("_" + key + ".prevNode.modifiedIndex", String.valueOf(prevNode.getInt("modifiedIndex")));
        }
        if (prevNode.containsKey("expiration")) {
            result.put("_" + key + ".prevNode.expiration", String.valueOf(prevNode.getString("expiration")));
        }
        if (prevNode.containsKey("ttl")) {
            result.put("_" + key + ".prevNode.ttl", String.valueOf(prevNode.getInt("ttl")));
        }
        result.put("_" + key + ".prevNode.value", prevNode.getString("value"));
    }
}

From source file:GUI_The_Code_Book.ParserAPIStackEx.java

private void parseStackExchange(String jsonStr) {
    JsonReader reader = null;// ww w .  j a  v a2 s  . c o m
    try {
        reader = Json.createReader(new StringReader(jsonStr));
        JsonObject jsonObject = reader.readObject();
        reader.close();
        JsonArray array = jsonObject.getJsonArray("items");
        for (JsonObject result : array.getValuesAs(JsonObject.class)) {
            urlList.add(new URLlist(result.getInt("view_count"), result.getInt("answer_count"),
                    result.getString("title"), result.getString("link")));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Becareful result is not unmarshalled/*w  w  w . j  ava 2s.  co  m*/
 *
 * @param json
 * @return
 */
protected static MessageToClient mtcFromJson(String json) {
    try (JsonReader reader = Json.createReader(new StringReader(json))) {
        JsonObject root = reader.readObject();
        MessageToClient message = new MessageToClient();
        message.setId(root.getString(Constants.Message.ID));
        message.setTime(root.getInt(Constants.Message.TIME));
        message.setType(MessageType.valueOf(root.getString(Constants.Message.TYPE)));
        message.setDeadline(root.getInt(Constants.Message.DEADLINE));
        if (null != message.getType()) {
            switch (message.getType()) {
            case FAULT:
                JsonObject faultJs = root.getJsonObject(Constants.Message.RESPONSE);
                Fault f = Fault.createFromJson(faultJs.toString());
                message.setFault(f);
                break;
            case MESSAGE:
                message.setResult("" + root.get(Constants.Message.RESPONSE));
                message.setType(MessageType.MESSAGE);
                break;
            case CONSTRAINT:
                JsonArray result = root.getJsonArray(Constants.Message.RESPONSE);
                List<ConstraintViolation> list = new ArrayList<>();
                for (JsonValue jsonValue : result) {
                    list.add(getJava(ConstraintViolation.class, ((JsonObject) jsonValue).toString()));
                }
                message.setConstraints(list.toArray(new ConstraintViolation[] {}));
                break;
            default:
                message.setResult("" + root.get(Constants.Message.RESPONSE));
                break;
            }
        }
        return message;
    }
}

From source file:prod.products.java

@POST
@Consumes("application/json")
public void post(String str) {
    JsonObject json = Json.createReader(new StringReader(str)).readObject();
    int newid = json.getInt("PRODUCT_ID");
    String id = String.valueOf(newid);
    String name = json.getString("PRODUCT_NAME");
    String description = json.getString("PRODUCT_DESCRIPTION");
    int newqty = json.getInt("QUANTITY");
    String qty = String.valueOf(newqty);
    System.out.println(id + name + description + qty);
    doUpdate(/*  w  w  w  . j  av a 2 s . com*/
            "INSERT INTO PRODUCT (PRODUCT_ID, PRODUCT_NAME, PRODUCT_DESCRIPTION, QUANTITY) VALUES (?, ?, ?, ?)",
            id, name, description, qty);
}

From source file:prod.products.java

@PUT
@Consumes("application/json")
public void put(String str) {
    JsonObject json = Json.createReader(new StringReader(str)).readObject();
    int newid = json.getInt("PRODUCT_ID");
    String id = String.valueOf(newid);
    String name = json.getString("PRODUCT_NAME");
    String description = json.getString("PRODUCT_DESCRIPTION");
    int newqty = json.getInt("QUANTITY");
    String qty = String.valueOf(newqty);
    System.out.println(id + name + description + qty);
    doUpdate(/*from   www .  j a v a  2  s  .c o  m*/
            "UPDATE PRODUCT SET PRODUCT_ID= ?, PRODUCT_NAME = ?, PRODUCT_DESCRIPTION = ?, QUANTITY = ? WHERE PRODUCT_ID = ?",
            id, name, description, qty, id);
}