Example usage for org.json.simple JSONArray stream

List of usage examples for org.json.simple JSONArray stream

Introduction

In this page you can find the example usage for org.json.simple JSONArray stream.

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:com.speedment.examples.social.JSONUser.java

public static List<JSONUser> parse(String json) {
    final JSONObject container = (JSONObject) JSONValue.parse(json);
    final JSONArray array = (JSONArray) container.get("users");
    final List<JSONUser> users = new ArrayList<>();

    array.stream().forEach(u -> {
        users.add(parse((JSONObject) u));
    });/* w w w.ja  va  2s. com*/

    return users;
}

From source file:com.speedment.examples.social.JSONImage.java

public static List<JSONImage> parseFrom(String json) {

    final JSONObject container = (JSONObject) JSONValue.parse(json);
    final JSONArray array = (JSONArray) container.get("images");
    final List<JSONImage> images = new ArrayList<>();

    array.stream().forEach(o -> {
        final JSONObject obj = (JSONObject) o;
        final JSONImage img = new JSONImage();
        final long time = Long.parseLong(obj.get("uploaded").toString());

        final LocalDateTime ldt = LocalDateTime.ofEpochSecond(time / 1000L, (int) (time % 1000) * 1000,
                ZoneOffset.UTC);/*from  www  .  j  ava2 s  . c o m*/

        img.title = obj.get("title").toString();
        img.description = obj.get("description").toString();
        img.uploaded = ldt;
        img.uploader = JSONUser.parse((JSONObject) obj.get("uploader"));
        img.image = fromBase64(obj.get("img_data").toString());
        images.add(img);
    });

    Collections.sort(images, Comparator.reverseOrder());

    return images;
}

From source file:cc.sferalabs.libs.telegram.bot.api.TelegramBot.java

/**
 * Sends a 'getUpdates' requests with the specified parameters to receive
 * incoming updates using long polling./*  w ww  .java2 s . c  o m*/
 * 
 * @param offset
 *            the offset parameter
 * @param limit
 *            the limit parameter
 * @param timeout
 *            the timeout parameter
 * @return a list containing the received updates
 * @throws ResponseError
 *             if the server returned an error response
 * @throws ParseException
 *             if an error occurs while parsing the server response
 * @throws IOException
 *             if an I/O exception occurs
 */
@SuppressWarnings("unchecked")
public List<Update> pollUpdates(Integer offset, Integer limit, Integer timeout)
        throws IOException, ParseException, ResponseError {
    JSONArray updates = sendRequest(new GetUpdatesRequest(offset, limit, timeout),
            timeout == null ? 5000 : timeout * 1000 + 5000);
    return (List<Update>) updates.stream().map(u -> new Update((JSONObject) u)).collect(Collectors.toList());
}

From source file:org.esa.s3tbx.olci.radiometry.rayleigh.RayleighAux.java

public static double[] parseJSON1DimArray(JSONObject parse, String ray_coeff_matrix) {
    JSONArray theta = (JSONArray) parse.get(ray_coeff_matrix);
    List<Double> collect = (List<Double>) theta.stream().collect(Collectors.toList());
    return Doubles.toArray(collect);
}

From source file:org.esa.s3tbx.olci.radiometry.rayleigh.RayleighAux.java

static ArrayList<double[][][]> parseJSON3DimArray(JSONObject parse, String ray_coeff_matrix) {
    JSONArray theta = (JSONArray) parse.get(ray_coeff_matrix);
    Iterator<JSONArray> iterator1 = theta.iterator();

    double[][][] rayCooffA = new double[3][12][12];
    double[][][] rayCooffB = new double[3][12][12];
    double[][][] rayCooffC = new double[3][12][12];
    double[][][] rayCooffD = new double[3][12][12];

    int k = 0;//from w w  w .  j  a v  a2  s .c o  m
    while (iterator1.hasNext()) { //3
        JSONArray next = iterator1.next();
        Iterator<JSONArray> iterator2 = next.iterator();
        int i1 = 0;
        while (iterator2.hasNext()) {//12
            JSONArray iterator3 = iterator2.next();
            Iterator<JSONArray> iterator4 = iterator3.iterator();
            for (int j = 0; j < 12; j++) {//12
                JSONArray mainValue = iterator4.next();
                List<Double> collectedValues = (List<Double>) mainValue.stream().collect(Collectors.toList());
                rayCooffA[k][i1][j] = collectedValues.get(0);
                rayCooffB[k][i1][j] = collectedValues.get(1);
                rayCooffC[k][i1][j] = collectedValues.get(2);
                rayCooffD[k][i1][j] = collectedValues.get(3);
            }
            i1++;
        }
        k++;
    }
    ArrayList<double[][][]> rayCoefficient = new ArrayList();
    rayCoefficient.add(rayCooffA);
    rayCoefficient.add(rayCooffB);
    rayCoefficient.add(rayCooffC);
    rayCoefficient.add(rayCooffD);
    return rayCoefficient;

}

From source file:org.ScripterRon.BitcoinMonitor.Utils.java

/**
 * Create a formatted string for a JSON structure
 *
 * @param       builder                 String builder
 * @param       indent                  Output indentation
 * @param       object                  The JSON object
 *//*from   w  ww  . java 2 s .com*/
private static void formatJSON(StringBuilder builder, String indent, JSONAware object) {
    String itemIndent = indent + "  ";
    if (object instanceof JSONArray) {
        JSONArray array = (JSONArray) object;
        builder.append(indent).append("[\n");
        array.stream().forEach((value) -> {
            if (value == null) {
                builder.append(itemIndent).append("null").append('\n');
            } else if (value instanceof Boolean) {
                builder.append(itemIndent).append((Boolean) value ? "true\n" : "false\n");
            } else if (value instanceof Long) {
                builder.append(itemIndent).append(((Long) value).toString()).append('\n');
            } else if (value instanceof Double) {
                builder.append(itemIndent).append(((Double) value).toString()).append('\n');
            } else if (value instanceof String) {
                builder.append(itemIndent).append('"').append((String) value).append("\"\n");
            } else if (value instanceof JSONAware) {
                builder.append('\n');
                formatJSON(builder, itemIndent + "  ", (JSONAware) value);
            } else {
                builder.append(itemIndent).append("Unknown\n");
            }
        });
        builder.append(indent).append("]\n");
    } else {
        builder.append(indent).append("{\n");
        JSONObject map = (JSONObject) object;
        Iterator<Map.Entry> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            builder.append(itemIndent).append("\"").append((String) entry.getKey()).append("\": ");
            Object value = entry.getValue();
            if (value == null) {
                builder.append("null").append('\n');
            } else if (value instanceof Boolean) {
                builder.append((Boolean) value ? "true\n" : "false\n");
            } else if (value instanceof Long) {
                builder.append(((Long) value).toString()).append('\n');
            } else if (value instanceof Double) {
                builder.append(((Double) value).toString()).append('\n');
            } else if (value instanceof String) {
                builder.append('"').append((String) value).append("\"\n");
            } else if (value instanceof JSONAware) {
                builder.append('\n');
                formatJSON(builder, itemIndent + "  ", (JSONAware) value);
            } else {
                builder.append("Unknown\n");
            }
        }
        builder.append(indent).append("}\n");
    }
}