Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

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

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

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

public Collection<Bundlable> getCollection(String key) {

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

    try {/*from  w w  w . j  a  va 2 s .  co m*/
        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);
            }//from w  ww  . j  a v  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;//from w w w  .j  av a 2  s  .  co  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   www  . ja  v a  2s  .  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: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)));
    }/* www  .  j  a va2s. 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 a2s.  com
    return resultVector;
}

From source file:org.collectionspace.chain.csp.persistence.services.XmlJsonConversion.java

private static JSONArray addRepeatedNodeToJson(Element container, Repeat f, String permlevel,
        JSONObject tempSon) throws JSONException {
    JSONArray node = new JSONArray();
    List<FieldSet> children = getChildrenWithGroupFields(f, permlevel);
    JSONArray elementlist = extractRepeatData(container, f, permlevel);

    JSONObject siblingitem = new JSONObject();
    for (int i = 0; i < elementlist.length(); i++) {
        JSONObject element = elementlist.getJSONObject(i);

        Iterator<?> rit = element.keys();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            JSONArray arrvalue = new JSONArray();
            for (FieldSet fs : children) {

                if (fs instanceof Repeat && ((Repeat) fs).hasServicesParent()) {
                    if (!((Repeat) fs).getServicesParent()[0].equals(key)) {
                        continue;
                    }/*from   w  w w.  j a  va2s. co m*/
                    Object value = element.get(key);
                    arrvalue = (JSONArray) value;
                } else {
                    if (!fs.getID().equals(key)) {
                        continue;
                    }
                    Object value = element.get(key);
                    arrvalue = (JSONArray) value;
                }

                if (fs instanceof Field) {
                    for (int j = 0; j < arrvalue.length(); j++) {
                        JSONObject repeatitem = new JSONObject();
                        //XXX remove when service layer supports primary tags
                        if (f.hasPrimary() && j == 0) {
                            repeatitem.put("_primary", true);
                        }
                        Element child = (Element) arrvalue.get(j);
                        Object val = child.getText();
                        Field field = (Field) fs;
                        String id = field.getID();
                        if (f.asSibling()) {
                            addExtraToJson(siblingitem, child, field, tempSon);
                            if (field.getDataType().equals("boolean")) {
                                siblingitem.put(id, (Boolean.parseBoolean((String) val) ? true : false));
                            } else {
                                siblingitem.put(id, val);
                            }
                        } else {
                            addExtraToJson(repeatitem, child, field, tempSon);
                            if (field.getDataType().equals("boolean")) {
                                repeatitem.put(id, (Boolean.parseBoolean((String) val) ? true : false));
                            } else {
                                repeatitem.put(id, val);
                            }
                            node.put(repeatitem);
                        }

                        tempSon = addtemp(tempSon, fs.getID(), child.getText());
                    }
                } else if (fs instanceof Group) {
                    JSONObject tout = new JSONObject();
                    JSONObject tempSon2 = new JSONObject();
                    Group rp = (Group) fs;
                    addRepeatToJson(tout, container, rp, permlevel, tempSon2, "", "");

                    if (f.asSibling()) {
                        JSONArray a1 = tout.getJSONArray(rp.getID());
                        JSONObject o1 = a1.getJSONObject(0);
                        siblingitem.put(fs.getID(), o1);
                    } else {
                        JSONObject repeatitem = new JSONObject();
                        repeatitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
                        node.put(repeatitem);
                    }

                    tempSon = addtemp(tempSon, rp.getID(), tout.getJSONArray(rp.getID()));
                    //log.info(f.getID()+":"+rp.getID()+":"+tempSon.toString());
                } else if (fs instanceof Repeat) {
                    JSONObject tout = new JSONObject();
                    JSONObject tempSon2 = new JSONObject();
                    Repeat rp = (Repeat) fs;
                    addRepeatToJson(tout, container, rp, permlevel, tempSon2, "", "");

                    if (f.asSibling()) {
                        siblingitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
                    } else {
                        JSONObject repeatitem = new JSONObject();
                        repeatitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
                        node.put(repeatitem);
                    }

                    tempSon = addtemp(tempSon, rp.getID(), tout.getJSONArray(rp.getID()));
                    //log.info(f.getID()+":"+rp.getID()+":"+tempSon.toString());
                }
            }
        }
    }

    if (f.asSibling()) {
        node.put(siblingitem);
    }
    return node;
}

From source file:org.collectionspace.chain.csp.persistence.services.XmlJsonConversion.java

private static void addGroupToJson(JSONObject out, Element root, Group f, String operation, JSONObject tempSon,
        String csid, String ims_url) throws JSONException {
    String nodeName = f.getServicesTag();
    if (f.hasServicesParent()) {
        nodeName = f.getfullID();/*w w  w.  j a  va2s . c  om*/
        //XXX hack because of weird repeats in accountroles permroles etc
        if (f.getServicesParent().length == 0) {
            nodeName = f.getID();
        }
    }
    if (!f.isGrouped()) {
        Element el = root;
        if (f.getUIType().startsWith("groupfield") && f.getUIType().contains("selfrenderer")) {

            String parts[] = f.getUIType().split("/");
            Record subitems = f.getRecord().getSpec().getRecordByServicesUrl(parts[1]);

            JSONObject temp = new JSONObject();
            for (FieldSet fs : subitems.getAllServiceFieldTopLevel(operation, "common")) {
                addFieldSetToJson(temp, el, fs, operation, tempSon, csid, ims_url);
            }

            out.put(f.getID(), temp);
            return;
        }

        if (f.getUIType().startsWith("groupfield")) {
            String parts[] = f.getUIType().split("/");
            Record subitems = f.getRecord().getSpec().getRecordByServicesUrl(parts[1]);

            JSONObject temp = new JSONObject();
            for (FieldSet fs : subitems.getAllServiceFieldTopLevel(operation, "common")) {
                addFieldSetToJson(temp, el, fs, operation, tempSon, csid, ims_url);
            }

            out.put(f.getID(), temp);
        }
    } else {
        List<?> nodes = root.selectNodes(nodeName);
        if (nodes.size() == 0)
            return;

        // Only first element is important in group container
        for (Object repeatcontainer : nodes) {
            Element container = (Element) repeatcontainer;
            JSONArray repeatitem = addRepeatedNodeToJson(container, f, operation, tempSon);
            JSONObject repeated = repeatitem.getJSONObject(0);
            out.put(f.getID(), repeated);
        }
    }

}

From source file:org.collectionspace.chain.csp.persistence.services.XmlJsonConversion.java

private static void addRepeatToJson(JSONObject out, Element root, Repeat f, String permlevel,
        JSONObject tempSon, String csid, String ims_url) throws JSONException {
    if (f.getXxxServicesNoRepeat()) { //not a repeat in services yet but is a repeat in UI
        FieldSet[] fields = f.getChildren(permlevel);
        if (fields.length == 0)
            return;
        JSONArray members = new JSONArray();
        JSONObject data = new JSONObject();
        addFieldSetToJson(data, root, fields[0], permlevel, tempSon, csid, ims_url);
        members.put(data);/*from w  w w . j  av a2s  . c o m*/
        out.put(f.getID(), members);
        return;
    }
    String nodeName = f.getServicesTag();
    if (f.hasServicesParent()) {
        nodeName = f.getfullID();
        //XXX hack because of weird repeats in accountroles permroles etc
        if (f.getServicesParent().length == 0) {
            nodeName = f.getID();
        }
    }
    List<?> nodes = root.selectNodes(nodeName);
    if (nodes.size() == 0) {// add in empty primary tags and arrays etc to help UI
        if (f.asSibling()) {
            JSONObject repeated = new JSONObject();
            if (f.hasPrimary()) {
                repeated.put("_primary", true);
            }
            if (!out.has(f.getID())) {
                JSONArray temp = new JSONArray();
                out.put(f.getID(), temp);
            }
            out.getJSONArray(f.getID()).put(repeated);
        } else {
            JSONArray repeatitem = new JSONArray();
            out.put(f.getID(), repeatitem);
        }
        return;
    }

    // Only first element is important in container
    //except when we have repeating items
    int pos = 0;
    for (Object repeatcontainer : nodes) {
        pos++;
        Element container = (Element) repeatcontainer;
        if (f.asSibling()) {
            JSONArray repeatitem = addRepeatedNodeToJson(container, f, permlevel, tempSon);
            JSONArray temp = new JSONArray();
            if (!out.has(f.getID())) {
                out.put(f.getID(), temp);
            }
            for (int arraysize = 0; arraysize < repeatitem.length(); arraysize++) {
                JSONObject repeated = repeatitem.getJSONObject(arraysize);

                if (f.hasPrimary() && pos == 1) {
                    repeated.put("_primary", true);
                }
                out.getJSONArray(f.getID()).put(repeated);
            }
        } else {
            JSONArray repeatitem = addRepeatedNodeToJson(container, f, permlevel, tempSon);
            out.put(f.getID(), repeatitem);
        }
    }
}

From source file:nl.hnogames.domoticzapi.Parsers.DevicesParser.java

@Override
public void parseResult(String result) {

    try {//from w ww .  j a va2 s  .  com
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<DevicesInfo> mDevices = new ArrayList<>();

        if (jsonArray.length() > 0) {

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                mDevices.add(new DevicesInfo(row));
            }
        }

        if (idx == 999999)
            receiver.onReceiveDevices(mDevices);
        else {
            receiver.onReceiveDevice(getDevice(idx, mDevices, scene_or_group));
        }
    } catch (JSONException e) {
        Log.e(TAG, "DevicesParser JSON exception");
        e.printStackTrace();
        receiver.onError(e);
    }
}