Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

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

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

From source file:com.watabou.pixeldungeon.utils.Bundle.java

public boolean[] getBooleanArray(String key) {
    try {/*from   www .  j av  a 2 s  .  com*/
        JSONArray array = data.getJSONArray(key);
        int length = array.length();
        boolean[] result = new boolean[length];
        for (int i = 0; i < length; i++) {
            result[i] = array.getBoolean(i);
        }
        return result;
    } catch (JSONException e) {
        return null;
    }
}

From source file:com.watabou.pixeldungeon.utils.Bundle.java

public String[] getStringArray(String key) {
    try {//from  w w  w .  j a v a 2s.  c o m
        JSONArray array = data.getJSONArray(key);
        int length = array.length();
        String[] result = new String[length];
        for (int i = 0; i < length; i++) {
            result[i] = array.getString(i);
        }
        return result;
    } catch (JSONException e) {
        return null;
    }
}

From source file:com.watabou.pixeldungeon.utils.Bundle.java

public Collection<Bundlable> getCollection(String key) {

    ArrayList<Bundlable> list = new ArrayList<Bundlable>();

    try {/*  w  ww. j a  v  a  2s . c om*/
        JSONArray array = data.getJSONArray(key);
        for (int i = 0; i < array.length(); i++) {
            list.add(new Bundle(array.getJSONObject(i)).get());
        }
    } catch (JSONException e) {

    }

    return list;
}

From source file:com.whizzosoftware.hobson.scheduler.ical.ICalTask.java

public ICalTask(ActionManager actionManager, String providerId, VEvent event, TaskExecutionListener listener)
        throws InvalidVEventException {
    this.actionManager = actionManager;
    this.providerId = providerId;
    this.event = event;
    this.listener = listener;

    if (event != null) {
        // adjust start time if sun offset is set
        Property sunOffset = event.getProperty(PROP_SUN_OFFSET);
        if (sunOffset != null) {
            try {
                solarOffset = new SolarOffset(sunOffset.getValue());
            } catch (ParseException e) {
                throw new InvalidVEventException("Invalid X-SUN-OFFSET", e);
            }// ww w  .  j av  a  2s.  c o  m
        }

        // parse actions
        Property commentProp = event.getProperty("COMMENT");
        if (commentProp != null) {
            JSONArray arr = new JSONArray(new JSONTokener(commentProp.getValue()));
            for (int i = 0; i < arr.length(); i++) {
                JSONObject json = arr.getJSONObject(i);
                if (json.has("pluginId") && json.has("actionId")) {
                    addActionRef(json);
                } else {
                    throw new InvalidVEventException("Found scheduled event with no plugin and/or action");
                }
            }
        } else {
            throw new InvalidVEventException("ICalEventTask event must have a COMMENT property");
        }
    } else {
        throw new InvalidVEventException("ICalEventTask must have a non-null event");
    }
}

From source file:com.whizzosoftware.hobson.scheduler.ical.ICalTask.java

public ICalTask(ActionManager actionManager, String providerId, JSONObject json) {
    this.actionManager = actionManager;
    this.providerId = providerId;

    this.event = new VEvent();
    String id;/* w w w .  j  a  va 2s  .  c  o  m*/
    if (json.has("id")) {
        id = json.getString("id");
    } else {
        id = UUID.randomUUID().toString();
    }
    event.getProperties().add(new Uid(id));
    event.getProperties().add(new Summary(json.getString("name")));

    try {
        if (json.has("conditions")) {
            JSONArray conditions = json.getJSONArray("conditions");
            if (conditions.length() == 1) {
                JSONObject jc = conditions.getJSONObject(0);
                if (jc.has("start") && !jc.isNull("start")) {
                    event.getProperties().add(new DtStart(jc.getString("start")));
                }
                if (jc.has("recurrence") && !jc.isNull("recurrence")) {
                    event.getProperties().add(new RRule(jc.getString("recurrence")));
                }
                if (jc.has("sunOffset") && !jc.isNull("sunOffset")) {
                    event.getProperties().add(new XProperty(PROP_SUN_OFFSET, jc.getString("sunOffset")));
                }
            } else {
                throw new HobsonRuntimeException("ICalTasks only support one condition");
            }
        }
    } catch (ParseException e) {
        throw new HobsonRuntimeException("Error parsing recurrence rule", e);
    }

    try {
        if (json.has("actions")) {
            JSONArray actions = json.getJSONArray("actions");
            event.getProperties().add(new Comment(actions.toString()));
            for (int i = 0; i < actions.length(); i++) {
                addActionRef(actions.getJSONObject(i));
            }
        }
    } catch (Exception e) {
        throw new HobsonRuntimeException("Error parsing actions", e);
    }
}

From source file:ai.susi.mind.SusiSkill.java

/**
 * Generate a set of skills from a single skill definition. This may be possible if the skill contains an 'options'
 * object which creates a set of skills, one for each option. The options combine with one set of phrases
 * @param json - a multi-skill definition
 * @return a set of skills//from  w  ww .  j  ava  2  s .c  o  m
 */
public static List<SusiSkill> getSkills(JSONObject json) {
    if (!json.has("phrases"))
        throw new PatternSyntaxException("phrases missing", "", 0);
    final List<SusiSkill> skills = new ArrayList<>();
    if (json.has("options")) {
        JSONArray options = json.getJSONArray("options");
        for (int i = 0; i < options.length(); i++) {
            JSONObject option = new JSONObject();
            option.put("phrases", json.get("phrases"));
            JSONObject or = options.getJSONObject(i);
            for (String k : or.keySet())
                option.put(k, or.get(k));
            skills.add(new SusiSkill(option));
        }
    } else {
        try {
            SusiSkill skill = new SusiSkill(json);
            skills.add(skill);
        } catch (PatternSyntaxException e) {
            Logger.getLogger("SusiSkill")
                    .warning("Regular Expression error in Susi Skill: " + json.toString(2));
        }
    }
    return skills;
}

From source file:ai.susi.mind.SusiSkill.java

/**
 * Create a skill by parsing of the skill description
 * @param json the skill description//from ww  w . j  a va 2 s. com
 * @throws PatternSyntaxException
 */
private SusiSkill(JSONObject json) throws PatternSyntaxException {

    // extract the phrases and the phrases subscore
    if (!json.has("phrases"))
        throw new PatternSyntaxException("phrases missing", "", 0);
    JSONArray p = (JSONArray) json.remove("phrases");
    this.phrases = new ArrayList<>(p.length());
    p.forEach(q -> this.phrases.add(new SusiPhrase((JSONObject) q)));

    // extract the actions and the action subscore
    if (!json.has("actions"))
        throw new PatternSyntaxException("actions missing", "", 0);
    p = (JSONArray) json.remove("actions");
    this.actions = new ArrayList<>(p.length());
    p.forEach(q -> this.actions.add(new SusiAction((JSONObject) q)));

    // extract the inferences and the process subscore; there may be no inference at all
    if (json.has("process")) {
        p = (JSONArray) json.remove("process");
        this.inferences = new ArrayList<>(p.length());
        p.forEach(q -> this.inferences.add(new SusiInference((JSONObject) q)));
    } else {
        this.inferences = new ArrayList<>(0);
    }

    // extract (or compute) the keys; there may be none key given, then they will be computed
    this.keys = new HashSet<>();
    JSONArray k;
    if (json.has("keys")) {
        k = json.getJSONArray("keys");
        if (k.length() == 0 || (k.length() == 1 && k.getString(0).length() == 0))
            k = computeKeysFromPhrases(this.phrases);
    } else {
        k = computeKeysFromPhrases(this.phrases);
    }

    k.forEach(o -> this.keys.add((String) o));

    this.user_subscore = json.has("score") ? json.getInt("score") : DEFAULT_SCORE;
    this.score = null; // calculate this later if required

    // extract the comment
    this.comment = json.has("comment") ? json.getString("comment") : "";

    // calculate the id
    String ids0 = this.actions.toString();
    String ids1 = this.phrases.toString();
    this.id = ids0.hashCode() + ids1.hashCode();
}

From source file:org.dasein.cloud.joyent.SmartDataCenter.java

public @Nonnull String getEndpoint() throws CloudException, InternalException {
    ProviderContext ctx = getContext();/*from   w  ww. j  a va 2 s.c o  m*/

    if (ctx == null) {
        throw new CloudException("No context has been established for this request");
    }
    String e = ctx.getEndpoint();

    if (e == null) {
        e = "https://us-west-1.api.joyentcloud.com";
    }
    String[] parts = e.split(",");

    if (parts == null || parts.length < 1) {
        parts = new String[] { e };
    }
    String r = ctx.getRegionId();

    if (r == null) {
        return parts[0];
    }
    if (endpointCache.containsKey(e)) {
        Map<String, String> cache = endpointCache.get(e);

        if (cache != null && cache.containsKey(r)) {
            String endpoint = cache.get(r);

            if (endpoint != null) {
                return endpoint;
            }
        }
    }
    JoyentMethod method = new JoyentMethod(this);

    String json = method.doGetJson(parts[0], "datacenters");
    try {
        JSONObject ob = new JSONObject(json);
        JSONArray ids = ob.names();

        for (int i = 0; i < ids.length(); i++) {
            String regionId = ids.getString(i);

            if (regionId.equals(r) && ob.has(regionId)) {
                String endpoint = ob.getString(regionId);
                Map<String, String> cache;

                if (endpointCache.containsKey(e)) {
                    cache = endpointCache.get(e);
                } else {
                    cache = new HashMap<String, String>();
                    endpointCache.put(e, cache);
                }
                cache.put(r, endpoint);
                return endpoint;
            }
        }
        throw new CloudException("No endpoint exists for " + r);
    } catch (JSONException ex) {
        throw new CloudException(ex);
    }
}

From source file:to.networld.fbtosemweb.fb.FacebookAgent.java

public Vector<FacebookEmployerEntity> getWork() throws JSONException {
    JSONArray workArray = this.agent.getJSONArray("work");
    Vector<FacebookEmployerEntity> resultVector = new Vector<FacebookEmployerEntity>();
    for (int count = 0; count < workArray.length(); count++) {
        resultVector.add(new FacebookEmployerEntity(workArray.getJSONObject(count)));
    }// w ww  . j  av  a 2 s  .co  m
    return resultVector;
}

From source file:to.networld.fbtosemweb.fb.FacebookAgent.java

public Vector<FacebookEducationEntity> getEducation() throws JSONException {
    JSONArray educationArray = this.agent.getJSONArray("education");
    Vector<FacebookEducationEntity> resultVector = new Vector<FacebookEducationEntity>();
    for (int count = 0; count < educationArray.length(); count++) {
        resultVector.add(new FacebookEducationEntity(educationArray.getJSONObject(count)));
    }/*from   w  w  w.  j a v a  2 s .com*/
    return resultVector;
}