Example usage for javax.json Json createReader

List of usage examples for javax.json Json createReader

Introduction

In this page you can find the example usage for javax.json Json createReader.

Prototype

public static JsonReader createReader(InputStream in) 

Source Link

Document

Creates a JSON reader from a byte stream.

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();//  w  ww . ja  v  a 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:org.json.StackExchangeAPI.java

private static void parseStackExchange(String jsonStr) {
    JsonReader reader = null;//  w  w  w  .  jav  a  2s  .  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)) {

            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.hakbot.util.JsonUtil.java

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

From source file:co.runrightfast.core.utils.JsonUtils.java

static javax.json.JsonObject toJsonObject(@NonNull final JsonObject json) {
    try (final JsonReader reader = Json.createReader(new StringReader(json.encode()))) {
        return reader.readObject();
    }//w  w w  .  j  a  v a2s.com
}

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

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

From source file:co.runrightfast.core.utils.JsonUtils.java

static javax.json.JsonObject parse(final String json) {
    checkArgument(isNotBlank(json));//www. j av  a2s  .com
    try (final JsonReader reader = Json.createReader(new StringReader(json))) {
        return reader.readObject();
    }
}

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   www. ja  va 2 s. c  om*/
 * @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:com.mac.holdempoker.socket.MessageDecoder.java

/**
 * Transform the input string into a Message
 * @param string/*from   w w  w .jav a 2 s  .  c  om*/
 * @return 
 * @throws javax.websocket.DecodeException
 */
@Override
public Message decode(String string) throws DecodeException {
    System.out.println("Decoding...");
    JsonObject json = Json.createReader(new StringReader(string)).readObject();
    System.out.println(json);
    return new Message(json);
}

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

/**
 * Creates a JsonArray from a json-formatted string
 *///w  w w.  j  ava2 s  .c  om
public static JsonArray toJsonArray(String jsonString) {
    JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
    return jsonReader.readArray();
}

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")));
                }/* w  w  w. j  a va 2  s .co m*/
            }
        } else if (serializedLicensesString.startsWith("{")) {
            readLegacyJson(serializedLicensesString, licenses);
        } else {
            readLegaySeparated(serializedLicensesString, licenses);
        }
    }
    return licenses;
}