Example usage for org.json.simple JSONArray get

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

Introduction

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

Prototype

public E get(int index) 

Source Link

Document

Returns the element at the specified position in this list.

Usage

From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java

/**
 * Returns a particular property from a JSONArray
 *
 * @param jsonArray  JSONArray you want to parse
 * @param simpleProp property you're searching for
 * @return An Object matching the required property
 */// w w w  .j  a va 2 s.  c om
private static Object getProperty(@NonNull JSONArray jsonArray, @NonNull String simpleProp) {
    if (simpleProp.startsWith("[")) {
        final int idx2 = simpleProp.indexOf("]");
        if (idx2 > 0) {
            final String exp = simpleProp.substring(1, idx2);
            if (exp.contains("=")) {
                final String[] expParams = exp.split("=");
                for (final Iterator<JSONObject> iterator = jsonArray.iterator(); iterator.hasNext();) {
                    final JSONObject jsonObject = iterator.next();
                    if (getValue(jsonObject, expParams[0]).equals(expParams[1]))
                        return jsonObject;
                }
            } else if (StringUtils.isNumeric(exp) && jsonArray.size() > Integer.parseInt(exp))
                return jsonArray.get(Integer.parseInt(exp));
        }
    }
    return null;
}

From source file:bolt.DucksBoardMap.java

public DucksBoardMap(String json) {
    Object obj = JSONValue.parse(json);
    JSONArray array = (JSONArray) obj;
    JSONObject obj2 = (JSONObject) array.get(0);
    this.idString = obj2.get("id").toString();
    this.keyString = obj2.get("key").toString();
    JSONArray fieldsJson = (JSONArray) obj2.get("requiredFields");
    String[] fieldsTmp = new String[fieldsJson.size()];
    //if (fieldsJson.size() == 0) 
    for (int i = 0; i < fieldsJson.size(); i++) {
        fieldsTmp[i] = (String) fieldsJson.get(i);
    }//from w w w.ja  va  2s. c  om

    this.fields = fieldsTmp;

}

From source file:bolt.DucksBoardTimeline.java

public DucksBoardTimeline(String json) {
    Object obj = JSONValue.parse(json);
    JSONArray array = (JSONArray) obj;
    JSONObject obj2 = (JSONObject) array.get(0);
    this.idString = obj2.get("id").toString();
    this.keyString = obj2.get("key").toString();
    JSONArray fieldsJson = (JSONArray) obj2.get("requiredFields");
    String[] fieldsTmp = new String[fieldsJson.size()];
    //if (fieldsJson.size() == 0) 
    for (int i = 0; i < fieldsJson.size(); i++) {
        fieldsTmp[i] = (String) fieldsJson.get(i);
    }//  w w w .jav  a 2  s . c  o  m

    this.fields = fieldsTmp;

}

From source file:Host.java

public Host(JSONArray list) {
    this.status = "ok";
    for (int i = 0; i < list.size(); i++) {
        JSONObject obj = (JSONObject) list.get(i);
        this.list.add(obj);
    }//from   ww  w .j ava  2s.c om
}

From source file:com.ire.people.Description.java

public Description(Object jsonDesc) {
    try {//  w  ww . j  av  a  2  s  . c  o  m
        JSONObject descJsonObject = (JSONObject) jsonDesc;
        JSONArray values = (JSONArray) descJsonObject.get("values");
        JSONObject firstValue = (JSONObject) values.get(0);
        JSONObject citation = (JSONObject) firstValue.get("citation");
        desc = firstValue.get("value").toString();
        if (citation != null) {
            provider = citation.get("provider").toString();
            providerURL = citation.get("uri").toString();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

}

From source file:com.intel.genomicsdb.GenomicsDBImporter.java

/**
 * Utility function that returns a list of ChromosomeInterval objects for
 * the column partition specified by the loader JSON file and rank/partition index
 * @param loaderJSONFile path to loader JSON file
 * @param partitionIdx rank/partition index
 * @return list of ChromosomeInterval objects for the specified partition 
 * @throws ParseException when there is a bug in the JNI interface and a faulty JSON is returned
 *///from   www  .j a v  a2s . com
public static ArrayList<ChromosomeInterval> getChromosomeIntervalsForColumnPartition(
        final String loaderJSONFile, final int partitionIdx) throws ParseException {
    final String chromosomeIntervalsJSONString = jniGetChromosomeIntervalsForColumnPartition(loaderJSONFile,
            partitionIdx);
    /* JSON format
      {
        "contigs": [
           { "chr1": [ 100, 200] },
           { "chr2": [ 500, 600] }
        ]
      }
    */
    ArrayList<ChromosomeInterval> chromosomeIntervals = new ArrayList<ChromosomeInterval>();
    JSONParser parser = new JSONParser();
    JSONObject topObj = (JSONObject) (parser.parse(chromosomeIntervalsJSONString));
    assert topObj.containsKey("contigs");
    JSONArray listOfDictionaries = (JSONArray) (topObj.get("contigs"));
    for (Object currDictObj : listOfDictionaries) {
        JSONObject currDict = (JSONObject) currDictObj;
        assert currDict.size() == 1; //1 entry
        for (Object currEntryObj : currDict.entrySet()) {
            Map.Entry<String, JSONArray> currEntry = (Map.Entry<String, JSONArray>) currEntryObj;
            JSONArray currValue = currEntry.getValue();
            assert currValue.size() == 2;
            chromosomeIntervals.add(new ChromosomeInterval(currEntry.getKey(), (Long) (currValue.get(0)),
                    (Long) (currValue.get(1))));
        }
    }
    return chromosomeIntervals;
}

From source file:ab.server.Proxy.java

@Override
public void onMessage(WebSocket conn, String message) {
    JSONArray j = (JSONArray) JSONValue.parse(message);
    Long id = (Long) j.get(0);
    JSONObject data = (JSONObject) j.get(1);

    ProxyResult<?> result = results.get(id);

    if (result != null) {
        results.remove(id);/*from  w  w w . j ava 2  s.c o  m*/
        try {
            result.queue.put(result.message.gotResponse(data));
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
    }
}

From source file:com.noelportugal.amazonecho.WitAi.java

public String getStatus(String text) {
    String ret = "off";
    try {/*from   w  ww.  j a  v a2s  .  c o m*/

        String json = getJson(text);

        Object obj = JSONValue.parse(json);
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray values = (JSONArray) jsonObject.get("outcomes");
        JSONObject outcome = (JSONObject) values.get(0);

        JSONObject on_off = (JSONObject) outcome.get("entities");
        JSONArray on_off_values = (JSONArray) on_off.get("on_off");
        JSONObject value = (JSONObject) on_off_values.get(0);
        ret = value.get("value").toString();

    } catch (Exception e) {
        ret = "off";
    }

    return ret.toLowerCase();
}

From source file:com.player.mood.moodplayer.soundcloud.SoundcloudPlaylist.java

public ArrayList<SoundcloudTrack> getSongsInfo() {
    String id, title, artist, streamURL, locationURL = "";
    Long duration;/*from w  ww  .j  a v a 2s .  co  m*/
    ArrayList<SoundcloudTrack> result = new ArrayList<SoundcloudTrack>();
    JSONArray tracks = (JSONArray) this.obj.get("tracks");
    JSONObject t = null;
    for (int i = 0; i < tracks.size(); i++) {
        t = (JSONObject) tracks.get(i);
        id = String.valueOf(t.get("id"));
        title = filterTitle(t.get("title").toString());
        artist = ((JSONObject) t.get("user")).get("username").toString();
        streamURL = t.get("stream_url").toString();
        duration = (Long) t.get("duration");

        SoundcloudTrack si = new SoundcloudTrack(id, artist, title, streamURL, duration);

        result.add(si);
    }
    return result;
}

From source file:com.noelportugal.amazonecho.WitAi.java

public WitEntity getEntity(String text) {
    WitEntity witEntity = new WitEntity();

    try {//from   w ww  .ja v  a 2  s  .  c o  m
        String json = getJson(text);

        //System.out.println(json);

        Object obj = JSONValue.parse(json);
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray values = (JSONArray) jsonObject.get("outcomes");
        JSONObject outcome = (JSONObject) values.get(0);

        JSONObject entities = (JSONObject) outcome.get("entities");

        JSONArray subject = (JSONArray) entities.get("message_subject");
        JSONObject subject_value = (JSONObject) subject.get(0);
        witEntity.setSubject(subject_value.get("value").toString());

        JSONArray state = (JSONArray) entities.get("on_off");
        JSONObject state_value = (JSONObject) state.get(0);
        witEntity.setState(state_value.get("value").toString());

    } catch (Exception e) {
        witEntity = null;
    }

    return witEntity;
}