Example usage for org.json.simple JSONArray size

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of elements in this list.

Usage

From source file:com.rackspacecloud.blueflood.outputs.serializers.JSONBasicRollupOutputSerializerTest.java

@Test
public void testTransformRollupDataAtFullRes() throws Exception {
    final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer();
    final MetricData metricData = new MetricData(FakeMetricDataGenerator.generateFakeFullResPoints(), "unknown",
            MetricData.Type.NUMBER);

    JSONObject metricDataJSON = serializer.transformRollupData(metricData, filterStats);

    final JSONArray data = (JSONArray) metricDataJSON.get("values");

    // Assert that we have some data to test
    Assert.assertTrue(data.size() > 0);

    for (int i = 0; i < data.size(); i++) {
        final JSONObject dataJSON = (JSONObject) data.get(i);
        final Points.Point<SimpleNumber> point = (Points.Point<SimpleNumber>) metricData.getData().getPoints()
                .get(dataJSON.get("timestamp"));

        Assert.assertEquals(point.getData().getValue(), dataJSON.get("average"));
        Assert.assertEquals(point.getData().getValue(), dataJSON.get("min"));
        Assert.assertEquals(point.getData().getValue(), dataJSON.get("max"));

        // Assert that variance isn't present
        Assert.assertNull(dataJSON.get("variance"));

        // Assert numPoints isn't present
        Assert.assertNull(dataJSON.get("numPoints"));
    }//w  w  w.  jav a2  s . c  o  m
}

From source file:com.michaeljones.hellohadoop.restclient.HadoopHdfsRestClient.java

public String[] ListDirectorySimple(String remoteRelativePath) {
    try {/*from  w ww .j  a  v  a2s . co m*/
        // %1 nameNodeHost, %2 username %3 resource.
        String uri = String.format(BASIC_URL_FORMAT, nameNodeHost, username, remoteRelativePath);
        List<Pair<String, String>> queryParams = new ArrayList();
        queryParams.add(new Pair<>("user.name", username));
        queryParams.add(new Pair<>("op", "LISTSTATUS"));

        String content = restImpl.GetStringContent(uri, queryParams);

        JSONParser parser = new JSONParser();
        JSONObject jsonObject = (JSONObject) parser.parse(content);

        JSONObject listStatus = (JSONObject) jsonObject.get("FileStatuses");
        JSONArray fileList = (JSONArray) listStatus.get("FileStatus");
        String[] directoryListing = new String[fileList.size()];
        int directoryIndex = 0;
        for (Object listing : fileList) {
            JSONObject jsonListing = (JSONObject) listing;
            String pathname = jsonListing.get("pathSuffix").toString();
            directoryListing[directoryIndex++] = pathname;
        }

        return directoryListing;
    } catch (ParseException ex) {
        LOGGER.error("Hadoop List directory failed: " + ex.getMessage());
        throw new RuntimeException("Hadoop List directory failed :" + ex.getMessage());
    } finally {
        restImpl.Close();
    }
}

From source file:com.capitalone.dashboard.util.ClientUtil.java

/**
 * Converts JSONArray to list artifact/*from w  ww. j av  a  2  s.c  om*/
 * 
 * @param array
 *            JSONArray artifact
 * @return A List artifact representing JSONArray information
 * @throws JSONException
 */
protected List<Object> toList(JSONArray array) throws JSONException {
    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < array.size(); i++) {
        Object value = array.get(i);
        if (value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }

        else if (value instanceof JSONObject) {
            value = this.toCanonicalSprintPOJO(value.toString());
        }
        list.add(value);
    }
    return list;
}

From source file:importer.Archive.java

/**
 * Using the project version info set long names for each short name
 * @param docid the project docid// ww  w.jav a  2 s .  com
 * @throws ImporterException 
 */
public void setVersionInfo(String docid) throws ImporterException {
    try {
        String project = Connector.getConnection().getFromDb(Database.PROJECTS, docid);
        if (project == null) {
            String[] parts = docid.split("/");
            if (parts.length == 3)
                docid = parts[0] + "/" + parts[1];
            else
                throw new Exception("Couldn't find project " + docid);
            project = Connector.getConnection().getFromDb(Database.PROJECTS, docid);
            if (project == null)
                throw new Exception("Couldn't find project " + docid);
        }
        JSONObject jObj = (JSONObject) JSONValue.parse(project);
        if (jObj.containsKey(JSONKeys.VERSIONS)) {
            JSONArray versions = (JSONArray) jObj.get(JSONKeys.VERSIONS);
            for (int i = 0; i < versions.size(); i++) {
                JSONObject version = (JSONObject) versions.get(i);
                Set<String> keys = version.keySet();
                Iterator<String> iter = keys.iterator();
                while (iter.hasNext()) {
                    String key = iter.next();
                    nameMap.put(key, (String) version.get(key));
                }
            }
        }
    } catch (Exception e) {
        throw new ImporterException(e);
    }
}

From source file:importer.handler.post.annotate.HTMLConverter.java

/**
 * Convert an xml->html converter with limited functionality
 * @param config the config specifying how to convert the tags+attributes
 *//*from   w ww .j  ava2s .  co m*/
HTMLConverter(JSONArray config) {
    this.body = new StringBuilder();
    this.patterns = config;
    map = new HashMap<Element, HTMLPattern>();
    this.stack = new Stack<HTMLPattern>();
    this.ignore = new HashSet<String>();
    for (int i = 0; i < config.size(); i++) {
        JSONObject jObj = (JSONObject) config.get(i);
        String htmlTag = (String) jObj.get("htmlTag");
        String xmlTag = (String) jObj.get("xmlTag");
        String htmlAttrName = (String) jObj.get("htmlAttrName");
        String htmlAttrValue = (String) jObj.get("htmlAttrValue");
        String xmlAttrName = (String) jObj.get("xmlAttrName");
        String xmlAttrValue = (String) jObj.get("xmlAttrValue");
        addPattern(xmlTag, htmlTag, htmlAttrName, htmlAttrValue, xmlAttrName, xmlAttrValue);
    }
}

From source file:functionaltests.RestSchedulerJobPaginationTest.java

private void checkRevisionJobsInfo(String url, String... expectedIds) throws Exception {
    Set<String> expected = new HashSet<>(Arrays.asList(expectedIds));
    Set<String> actual = new HashSet<>();

    JSONObject page = getRequestJSONObject(url);
    JSONObject map = (JSONObject) page.get("map");
    Assert.assertEquals(1, map.keySet().size());
    JSONArray jobs = (JSONArray) map.get(map.keySet().iterator().next());
    for (int i = 0; i < jobs.size(); i++) {
        JSONObject job = (JSONObject) jobs.get(i);
        actual.add((String) job.get("jobid"));
    }//from  w w  w.  jav a  2  s .c  o m
    Assert.assertEquals("Unexpected result of 'revisionjobsinfo' request (" + url + ")", expected, actual);
}

From source file:com.rackspacecloud.blueflood.outputs.serializers.JSONBasicRollupOutputSerializerTest.java

@Test
public void testTransformRollupDataForCoarserGran() throws Exception {
    final JSONBasicRollupsOutputSerializer serializer = new JSONBasicRollupsOutputSerializer();
    final MetricData metricData = new MetricData(FakeMetricDataGenerator.generateFakeRollupPoints(), "unknown",
            MetricData.Type.NUMBER);
    Set<MetricStat> filters = new HashSet<MetricStat>();
    filters.add(MetricStat.AVERAGE);/* ww w  .  j a v a  2s  .com*/
    filters.add(MetricStat.MIN);
    filters.add(MetricStat.MAX);
    filters.add(MetricStat.NUM_POINTS);

    JSONObject metricDataJSON = serializer.transformRollupData(metricData, filters);
    final JSONArray data = (JSONArray) metricDataJSON.get("values");

    // Assert that we have some data to test
    Assert.assertTrue(data.size() > 0);

    for (int i = 0; i < data.size(); i++) {
        final JSONObject dataJSON = (JSONObject) data.get(i);
        final Points.Point point = (Points.Point) metricData.getData().getPoints()
                .get(dataJSON.get("timestamp"));

        long numPoints = ((BasicRollup) point.getData()).getCount();
        Assert.assertEquals(numPoints, dataJSON.get("numPoints"));

        if (numPoints == 0) {
            Assert.assertNull(dataJSON.get("average"));
            Assert.assertNull(dataJSON.get("min"));
            Assert.assertNull(dataJSON.get("max"));
        } else {
            Assert.assertEquals(((BasicRollup) point.getData()).getAverage(), dataJSON.get("average"));
            Assert.assertEquals(((BasicRollup) point.getData()).getMaxValue(), dataJSON.get("max"));
            Assert.assertEquals(((BasicRollup) point.getData()).getMinValue(), dataJSON.get("min"));
        }

        // Assert that variance isn't present
        Assert.assertNull(dataJSON.get("variance"));
    }
}

From source file:data.GoogleBooksWS.java

@Override
public void parse(String tekst) throws Exception {

    //List<Book> lista=new ArrayList<>();

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(tekst);
    JSONObject jsonObject = (JSONObject) obj;
    if (prvi) {/*from  w  w w .  j av a2  s  .c o m*/
        int brojRezultata = Integer.parseInt(jsonObject.get("totalItems") + "");
        ukupanBrojStrana = brojRezultata / RESULTS_PER_PAGE + 1;
        System.out.println("Broj strana" + ukupanBrojStrana);
        prvi = false;
    }

    System.out.println("Trenutni broj strane " + trenutniBrojStrane);
    JSONArray niz = (JSONArray) jsonObject.get("items");
    if (niz == null) {
        kraj = true;
    } else {
        Iterator iterator = niz.iterator();
        while (iterator.hasNext()) {
            Book b = new Book();
            b.setUri(URIGenerator.generateUri(b));
            JSONObject book = (JSONObject) iterator.next();
            b.setTitle((String) ((JSONObject) book.get("volumeInfo")).get("title"));
            JSONArray autori = (JSONArray) ((JSONObject) book.get("volumeInfo")).get("authors");
            if (autori != null) {
                Iterator it = autori.iterator();
                while (it.hasNext()) {
                    Person p = new Person();
                    p.setUri(URIGenerator.generateUri(p));
                    p.setName((String) it.next());
                    b.getAuthors().add(p);
                }
            } else {
                System.out.println("Autori su null");
            }
            Organization o = new Organization();
            o.setUri(URIGenerator.generateUri(o));
            o.setName((String) ((JSONObject) book.get("volumeInfo")).get("publisher"));
            b.setPublisher(o);
            String date = (String) ((JSONObject) book.get("volumeInfo")).get("publishedDate");
            System.out.println(date);
            b.setDatePublished(date);

            String description = (String) ((JSONObject) book.get("volumeInfo")).get("description");
            b.setDescription(description);

            Object brStr = ((JSONObject) book.get("volumeInfo")).get("pageCount");
            if (brStr != null) {
                long broj = Long.parseLong(brStr.toString());
                b.setNumberOfPages((int) broj);
            }

            JSONArray identifiers = (JSONArray) ((JSONObject) book.get("volumeInfo"))
                    .get("industryIdentifiers");
            if (identifiers != null) {
                for (int i = 0; i < identifiers.size(); i++) {
                    JSONObject identifier = (JSONObject) identifiers.get(i);
                    if ("ISBN_13".equals((String) identifier.get("type"))) {
                        b.setIsbn((String) identifier.get("identifier"));
                    }
                }
            }
            //b.setIsbn((String) ((JSONObject) ((JSONArray)((JSONObject)book.get("volumeInfo")).get("industryIdentifiers")).get(1)).get("identifier"));

            lista.add(b);

        }

        if (trenutniBrojStrane < ukupanBrojStrana) {
            trenutniBrojStrane++;
        } else {
            kraj = true;
        }
    }
}

From source file:cc.pinel.mangue.storage.MangaStorage.java

public Collection getMangas() {
    Collection mangas = new ArrayList();

    JSONArray jsonMangas = null;
    try {//  w  ww. j ava 2 s  .  c o m
        jsonMangas = (JSONArray) readJSON().get("mangas");
    } catch (Exception e) {
        // ignored
    }
    if (jsonMangas == null)
        jsonMangas = new JSONArray();

    for (int i = 0; i < jsonMangas.size(); i++) {
        JSONObject jsonManga = (JSONObject) jsonMangas.get(i);
        mangas.add(new Manga(jsonManga.get("id").toString(), jsonManga.get("name").toString(),
                jsonManga.get("path").toString()));
    }

    return mangas;
}

From source file:net.bashtech.geobot.JSONUtil.java

public static ArrayList<String> BOIItems(String channel) {

    try {/*from  w  ww  .  ja va  2  s .  c  o m*/
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(
                BotManager.getRemoteContent("http://coebot.tv/configs/boir/" + channel.substring(1) + ".json"));

        JSONObject jsonObject = (JSONObject) obj;

        JSONArray items = (JSONArray) jsonObject.get("items");
        ArrayList<String> itemList = new ArrayList<String>();
        for (int i = 0; i < items.size(); i++) {
            itemList.add((String) items.get(i));
        }

        return itemList;
    } catch (Exception ex) {
        return null;
    }
}