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.liferay.mobile.android.v62.mdraction.MDRActionService.java

public JSONObject updateAction(long actionId, JSONObject nameMap, JSONObject descriptionMap, String type,
        JSONObjectWrapper typeSettingsProperties, JSONObjectWrapper serviceContext) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from w  w w.j  a  va2  s  .  co m
        JSONObject _params = new JSONObject();

        _params.put("actionId", actionId);
        _params.put("nameMap", checkNull(nameMap));
        _params.put("descriptionMap", checkNull(descriptionMap));
        _params.put("type", checkNull(type));
        mangleWrapper(_params, "typeSettingsProperties", "com.liferay.portal.kernel.util.UnicodeProperties",
                typeSettingsProperties);
        mangleWrapper(_params, "serviceContext", "com.liferay.portal.service.ServiceContext", serviceContext);

        _command.put("/mdraction/update-action", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}

From source file:org.protorabbit.Config.java

static void processURIResources(int type, JSONObject bsjo, ITemplate temp, String baseURI)
        throws JSONException {

    List<ResourceURI> refs = null;
    refs = new ArrayList<ResourceURI>();

    if (bsjo.has("libs")) {

        JSONArray ja = bsjo.getJSONArray("libs");

        for (int j = 0; j < ja.length(); j++) {

            JSONObject so = ja.getJSONObject(j);
            String url = so.getString("url");
            if (url.startsWith("/") || url.startsWith("http")) {
                baseURI = "";
            }/*w w  w  . j a  va2  s .c  o m*/
            ResourceURI ri = new ResourceURI(url, baseURI, type);
            if (so.has("id")) {
                ri.setId(so.getString("id"));
            }
            if (so.has("uaTest")) {
                ri.setUATest(so.getString("uaTest"));
            }
            if (so.has("test")) {
                ri.setTest(so.getString("test"));
            }
            if (so.has("defer")) {
                ri.setDefer(so.getBoolean("defer"));
            }
            if (so.has("combine")) {
                ri.setCombine(so.getBoolean("combine"));
            }
            if (so.has("uniqueURL")) {
                Boolean unique = so.getBoolean("uniqueURL");
                ri.setUniqueURL(unique);
            }
            refs.add(ri);
        }
    }
    Boolean combine = null;
    if (bsjo.has("combineResources")) {
        combine = bsjo.getBoolean("combineResources");
    }
    Boolean lgzip = null;
    if (bsjo.has("gzip")) {
        lgzip = bsjo.getBoolean("gzip");
    }

    if (type == ResourceURI.SCRIPT) {
        temp.setCombineScripts(combine);
        temp.setGzipScripts(lgzip);
        temp.setScripts(refs);
    } else if (type == ResourceURI.LINK) {
        temp.setGzipStyles(lgzip);
        temp.setStyles(refs);
        temp.setCombineStyles(combine);
    }
}

From source file:org.protorabbit.Config.java

public void registerTemplates(JSONArray templates, String baseURI) {

    for (int i = 0; i < templates.length(); i++) {
        try {/*from w w  w  . j ava 2  s .  c  om*/
            JSONObject t = templates.getJSONObject(i);
            String id = t.getString("id");
            ITemplate temp = new Template(id, baseURI, t, this);
            registerTemplates(temp, t, baseURI);
            tmap.put(id, temp);
            getLogger().info("Added template definition : " + temp.getId());

        } catch (JSONException e) {
            getLogger().log(Level.SEVERE, "Error parsing configuration.", e);
        }
    }
}

From source file:org.protorabbit.Config.java

@SuppressWarnings("unchecked")
private void registerTemplates(ITemplate temp, JSONObject t, String baseURI) {

    try {// w w  w  . ja  va 2s . co m

        if (t.has("timeout")) {
            long templateTimeout = t.getLong("timeout");
            temp.setTimeout(templateTimeout);
        }

        boolean tgzip = false;

        if (!devMode) {
            tgzip = gzip;
        }

        if (t.has("gzip")) {
            tgzip = t.getBoolean("gzip");
            temp.setGzipStyles(tgzip);
            temp.setGzipScripts(tgzip);
            temp.setGzipTemplate(tgzip);
        }

        if (t.has("uniqueURL")) {
            Boolean unique = t.getBoolean("uniqueURL");
            temp.setUniqueURL(unique);
        }

        // template overrides default combineResources
        if (t.has("combineResources")) {
            boolean combineResources = t.getBoolean("combineResources");
            temp.setCombineResources(combineResources);
            temp.setCombineScripts(combineResources);
            temp.setCombineStyles(combineResources);
        }

        if (t.has("template")) {
            String turi = t.getString("template");
            ResourceURI templateURI = new ResourceURI(turi, baseURI, ResourceURI.TEMPLATE);
            temp.setTemplateURI(templateURI);
        }

        if (t.has("namespace")) {
            temp.setURINamespace(t.getString("namespace"));
        }

        if (t.has("extends")) {
            List<String> ancestors = null;
            String base = t.getString("extends");
            if (base.length() > 0) {
                String[] parentIds = null;
                if (base.indexOf(",") != -1) {
                    parentIds = base.split(",");
                } else {
                    parentIds = new String[1];
                    parentIds[0] = base;
                }
                ancestors = new ArrayList<String>();

                for (int j = 0; j < parentIds.length; j++) {
                    ancestors.add(parentIds[j].trim());
                }
            }

            temp.setAncestors(ancestors);
        }

        if (t.has("overrides")) {

            List<TemplateOverride> overrides = new ArrayList<TemplateOverride>();
            JSONArray joa = t.getJSONArray("overrides");
            for (int z = 0; z < joa.length(); z++) {
                TemplateOverride tor = new TemplateOverride();
                JSONObject toro = joa.getJSONObject(z);
                if (toro.has("test")) {
                    tor.setTest(toro.getString("test"));
                }
                if (toro.has("uaTest")) {
                    tor.setUATest(toro.getString("uaTest"));
                }
                if (toro.has("import")) {
                    tor.setImportURI(toro.getString("import"));
                }
                overrides.add(tor);
            }
            temp.setTemplateOverrides(overrides);
        }

        if (t.has("scripts")) {

            JSONObject bsjo = t.getJSONObject("scripts");

            processURIResources(ResourceURI.SCRIPT, bsjo, temp, baseURI);
        }

        if (t.has("styles")) {
            JSONObject bsjo = t.getJSONObject("styles");

            processURIResources(ResourceURI.LINK, bsjo, temp, baseURI);
        }

        if (t.has("properties")) {

            Map<String, IProperty> properties = null;
            JSONObject po = t.getJSONObject("properties");
            properties = new HashMap<String, IProperty>();

            Iterator<String> jit = po.keys();
            while (jit.hasNext()) {

                String name = jit.next();
                JSONObject so = po.getJSONObject(name);
                int type = IProperty.STRING;
                String value = so.getString("value");

                if (so.has("type")) {

                    String typeString = so.getString("type");

                    if ("string".equals(typeString.toLowerCase())) {
                        type = IProperty.STRING;
                    } else if ("include".equals(typeString.toLowerCase())) {
                        type = IProperty.INCLUDE;
                    }
                }

                IProperty pi = new Property(name, value, type, baseURI, temp.getId());

                if (so.has("timeout")) {
                    long timeout = so.getLong("timeout");
                    pi.setTimeout(timeout);
                }
                if (so.has("id")) {
                    pi.setId(so.getString("id"));
                }
                if (so.has("uaTest")) {
                    pi.setUATest(so.getString("uaTest"));
                }
                if (so.has("test")) {
                    pi.setTest(so.getString("test"));
                }
                if (so.has("defer")) {
                    pi.setDefer(so.getBoolean("defer"));
                }
                if (so.has("deferContent")) {
                    pi.setDeferContent(new StringBuffer(so.getString("deferContent")));
                }
                properties.put(name, pi);
            }
            temp.setProperties(properties);
        }

    } catch (JSONException e) {
        getLogger().log(Level.SEVERE, "Error parsing configuration.", e);
    }

}

From source file:census.couchdroid.CouchViewResults.java

/**
 * Retrieves a list of documents that matched this View.
 * These documents only contain the data that the View has returned (not the full document).
 * <p>//  w  ww  .  j  a  v  a 2  s.com
 * You can load the remaining information from Document.reload();
 * 
 * @return
*/
public List<CouchDocument> getResults() {
    JSONArray ar = null;
    try {
        android.util.Log.e("Census", getJSONObject().toString());
        ar = getJSONObject().getJSONArray("rows");
    } catch (JSONException e) {
        return null;
    }
    List<CouchDocument> docs = new ArrayList<CouchDocument>(ar.length());
    for (int i = 0; i < ar.length(); i++) {
        try {
            if (ar.get(i) != null && !ar.getString(i).equals("null")) {
                CouchDocument d = new CouchDocument(ar.getJSONObject(i));
                d.setDatabase(database);
                docs.add(d);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return docs;

}

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

@Override
public void parseResult(String result) {
    try {/*from   w w w.  j  a v a 2s .co m*/
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<NotificationInfo> mNotificationInfo = new ArrayList<>();

        if (jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                mNotificationInfo.add(new NotificationInfo(row));
            }
        }
        notificationsReceiver.onReceiveNotifications(mNotificationInfo);

    } catch (JSONException e) {
        Log.e(TAG, "JSON exception");
        e.printStackTrace();
        notificationsReceiver.onError(e);
    }
}

From source file:com.pansapiens.occyd.JSONdecoder.java

/**
* Abstract utility class for parsing JSON objects (as strings) and
* returning Java objects./*from   ww w.  j  a va2 s .c  o m*/
*/
public static ArrayList<Post> json2postArray(String json_txt) throws JSONException {
    /**
     * Takes an appropriate JSON format string, returned as a response
     * by the server to a search query, returns an ArrayList
     *  of Post objects. 
     */
    ArrayList<Post> post_results = new ArrayList();

    if (json_txt != null) {
        // http://code.google.com/android/reference/org/json/JSONObject.html
        JSONTokener jsontok = new JSONTokener(json_txt);
        JSONObject json;
        String geohash = null;
        String[] tags = null;

        // TODO: read these from the JSON too
        //String key = null;
        //String user = null;
        //String desc = null;
        //URL link = null;
        // see: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html
        //Date date = null;
        // parse "2008-12-31 03:22:23.350798" format date with:
        // (could have an issue with to many millisecond places SSSSSS .... may need to truncate)
        // SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        // Date date = dateformat.parse(dateString);

        float lat, lon;

        json = new JSONObject(jsontok);

        // catch any error codes in the returned json
        String result = json.getString("result");
        if (!result.equals("done")) {
            return post_results;
        }

        // unpack the json into Post objects, add them to the result list
        JSONArray posts = json.getJSONArray("posts");
        for (int i = 0; i < posts.length(); i++) {
            JSONObject p = posts.getJSONObject(i);
            geohash = (String) p.get("geohash");
            JSONArray coordinates = p.getJSONArray("coordinates");
            lat = (float) coordinates.getDouble(0);
            lon = (float) coordinates.getDouble(1);

            JSONArray t = p.getJSONArray("tags");
            tags = new String[t.length()];
            for (int j = 0; j < t.length(); j++) {
                tags[j] = t.getString(j);
            }

            Post post = new Post(lat, lon, geohash, tags);
            post_results.add(post);
        }
    }
    return post_results;
}

From source file:net.jmhertlein.mcanalytics.console.gui.LoginPane.java

private void loadHostPanes(JSONObject config) {

    if (config.has("hosts")) {
        JSONArray hosts = config.getJSONArray("hosts");
        for (int i = 0; i < hosts.length(); i++) {
            JSONObject host = hosts.getJSONObject(i);
            HostEntry entry = HostEntry.fromJSON(host);
            try {
                entry.setHasCert(trust.containsAlias(entry.getUrl() + "-private"));
            } catch (KeyStoreException ex) {
                Logger.getLogger(LoginPane.class.getName()).log(Level.SEVERE, null, ex);
            }//from ww  w .j  a va2  s.c  o  m
            hostList.getItems().add(entry);
        }
    }

    if (!hostList.getItems().isEmpty())
        hostList.getSelectionModel().select(0);
}

From source file:conroller.CommentController.java

public static ArrayList<CommentModel> viewComment() throws IOException, JSONException {
    phpConnection.setConnection(/*from www  .ja v a 2 s . c o  m*/
            "http://itmahaweliauthority.net23.net/MahaweliAuthority/CommentPHPfiles/CommentGetView.php");
    String feedbackId, userName, typeOfUser, tankName, massage;
    ArrayList<CommentModel> arrayList = new ArrayList<>();
    JSONObject jSONObject = new JSONObject(phpConnection.readData());
    JSONArray array = jSONObject.getJSONArray("server_response");
    for (int i = 0; i < array.length(); i++) {
        JSONObject details = array.getJSONObject(i);
        try {
            feedbackId = String.valueOf(details.getString("feedbackId"));
        } catch (Exception ex) {
            feedbackId = null;
        }
        try {
            userName = String.valueOf(details.getString("userName"));
        } catch (Exception ex) {
            userName = null;
        }
        try {
            typeOfUser = String.valueOf(details.getString("typeOfUser"));
        } catch (Exception ex) {
            typeOfUser = null;
        }
        try {
            tankName = String.valueOf(details.getString("tankName"));
        } catch (Exception ex) {
            tankName = null;
        }
        try {
            massage = String.valueOf(details.getString("massage"));
        } catch (Exception ex) {
            massage = null;
        }
        CommentModel model = new CommentModel(feedbackId, userName, typeOfUser, tankName, massage);
        arrayList.add(model);
    }
    return arrayList;
}

From source file:de.knufficast.search.GPodderSearch.java

public void search(final String query, final BooleanCallback<List<Result>, String> callback) {
    runOnThread(new Runnable() {
        @Override//from w w  w  .  j  ava2s.co  m
        public void run() {
            String queryUrl = String.format(SEARCH_URL, URLEncoder.encode(query));
            HttpGet request = new HttpGet(queryUrl);
            try {
                List<Result> results = new ArrayList<Result>();
                JSONArray response = httpUtil.getJsonArray(request);
                int max = response.length();
                for (int i = 0; i < max; i++) {
                    JSONObject jsonResult = response.getJSONObject(i);
                    GPodderResult result = new GPodderResult();
                    result.website = jsonResult.optString("website");
                    result.description = jsonResult.optString("description");
                    result.title = jsonResult.optString("title");
                    result.feedUrl = jsonResult.optString("url");
                    result.imgUrl = jsonResult.optString("scaled_logo_url");
                    results.add(result);
                }
                callback.success(results);
            } catch (IOException e) {
                callback.fail(ERROR_CONNECTION);
            } catch (JSONException e) {
                callback.fail(ERROR_JSON);
            }
        }
    });
}