Example usage for org.json JSONObject names

List of usage examples for org.json JSONObject names

Introduction

In this page you can find the example usage for org.json JSONObject names.

Prototype

public JSONArray names() 

Source Link

Document

Produce a JSONArray containing the names of the elements of this JSONObject.

Usage

From source file:com.jennifer.ui.util.JSONUtil.java

public static JSONObject extend(JSONObject o, JSONObject attr) {
    JSONObject newObj = new JSONObject();

    JSONArray names1 = o.names();
    if (names1 != null) {
        for (int i = 0, len = names1.length(); i < len; i++) {
            String key = names1.getString(i);
            Object value = o.get(key);

            newObj.put(key, value);/*from  w w w.ja v a2 s  .c o m*/
        }
    }

    JSONArray names = attr.names();
    if (names != null) {
        for (int i = 0, len = names.length(); i < len; i++) {
            String key = names.getString(i);
            Object value = attr.get(key);

            newObj.put(key, value);
        }
    }

    return newObj;
}

From source file:at.alladin.rmbt.android.util.GetMapOptionsInfoTask.java

/**
 * //from  ww  w. j a v a 2s . c o  m
 */
@Override
protected void onPostExecute(final JSONObject result) {
    if (serverConn.hasError())
        hasError = true;
    else if (result != null) {
        try {
            final JSONObject mapSettingsObject = result.getJSONObject("mapfilter");

            // /

            // ////////////////////////////////////////////////////
            // MAP / CHOOSE

            final JSONArray mapTypeArray = mapSettingsObject.getJSONArray("mapTypes");

            //                Log.d(DEBUG_TAG, mapTypeArray.toString(4));

            // /

            final ArrayList<MapListSection> mapListSectionList = new ArrayList<MapListSection>();

            for (int cnt = 0; cnt < mapTypeArray.length(); cnt++) {

                final JSONObject t = mapTypeArray.getJSONObject(cnt);

                final String sectionTitle = t.getString("title");

                final JSONArray objectOptionsArray = t.getJSONArray("options");

                // /

                final List<MapListEntry> mapListEntryList = new ArrayList<MapListEntry>();

                for (int cnt2 = 0; cnt2 < objectOptionsArray.length(); cnt2++) {

                    final JSONObject s = objectOptionsArray.getJSONObject(cnt2);

                    final String entryTitle = s.getString("title");
                    final String entrySummary = s.getString("summary");
                    final String value = s.getString("map_options");
                    final String overlayType = s.getString("overlay_type");

                    final MapListEntry mapListEntry = new MapListEntry(entryTitle, entrySummary);

                    mapListEntry.setKey("map_options");
                    mapListEntry.setValue(value);
                    mapListEntry.setOverlayType(overlayType);

                    mapListEntryList.add(mapListEntry);
                }

                final MapListSection mapListSection = new MapListSection(sectionTitle, mapListEntryList);
                mapListSectionList.add(mapListSection);
            }

            // ////////////////////////////////////////////////////
            // MAP / FILTER

            final JSONObject mapFiltersObject = mapSettingsObject.getJSONObject("mapFilters");

            final HashMap<String, List<MapListSection>> mapFilterListSectionListHash = new HashMap<String, List<MapListSection>>();

            //                Log.d(DEBUG_TAG, mapFilterArray.toString(4));

            for (final String typeKey : new String[] { "mobile", "wifi", "browser" }) {
                final JSONArray mapFilterArray = mapFiltersObject.getJSONArray(typeKey);
                final List<MapListSection> mapFilterListSectionList = new ArrayList<MapListSection>();
                mapFilterListSectionListHash.put(typeKey, mapFilterListSectionList);

                // add map appearance option (satellite, no satellite)
                final MapListSection appearanceSection = new MapListSection(
                        activity.getString(R.string.map_appearance_header),
                        Arrays.asList(
                                new MapListEntry(activity.getString(R.string.map_appearance_nosat_title),
                                        activity.getString(R.string.map_appearance_nosat_summary), true,
                                        MapProperties.MAP_SAT_KEY, MapProperties.MAP_NOSAT_VALUE, true),
                                new MapListEntry(activity.getString(R.string.map_appearance_sat_title),
                                        activity.getString(R.string.map_appearance_sat_summary),
                                        MapProperties.MAP_SAT_KEY, MapProperties.MAP_SAT_VALUE)));

                mapFilterListSectionList.add(appearanceSection);

                // add overlay option (heatmap, points)
                final MapListSection overlaySection = new MapListSection(
                        activity.getString(R.string.map_overlay_header),
                        Arrays.asList(
                                new MapListEntry(
                                        activity.getString(R.string.map_overlay_auto_title),
                                        activity.getString(R.string.map_overlay_auto_summary), true,
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.AUTO.name(), true),
                                new MapListEntry(activity.getString(R.string.map_overlay_heatmap_title),
                                        activity.getString(R.string.map_overlay_heatmap_summary),
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.HEATMAP.name()),
                                new MapListEntry(activity.getString(R.string.map_overlay_points_title),
                                        activity.getString(R.string.map_overlay_points_summary),
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.POINTS.name()),
                                new MapListEntry(activity.getString(R.string.map_overlay_regions_title),
                                        activity.getString(R.string.map_overlay_regions_summary),
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.REGIONS.name()),
                                new MapListEntry(activity.getString(R.string.map_overlay_municipality_title),
                                        activity.getString(R.string.map_overlay_municipality_summary),
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.MUNICIPALITY.name()),
                                new MapListEntry(activity.getString(R.string.map_overlay_settlements_title),
                                        activity.getString(R.string.map_overlay_settlements_summary),
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.SETTLEMENTS.name()),
                                new MapListEntry(activity.getString(R.string.map_overlay_whitespots_title),
                                        activity.getString(R.string.map_overlay_whitespots_summary),
                                        MapProperties.MAP_OVERLAY_KEY, MapOverlay.WHITESPOTS.name())));

                mapFilterListSectionList.add(overlaySection);

                // add other filter options

                for (int cnt = 0; cnt < mapFilterArray.length(); cnt++) {

                    final JSONObject t = mapFilterArray.getJSONObject(cnt);

                    final String sectionTitle = t.getString("title");

                    final JSONArray objectOptionsArray = t.getJSONArray("options");

                    // /

                    final List<MapListEntry> mapListEntryList = new ArrayList<MapListEntry>();

                    boolean haveDefault = false;

                    for (int cnt2 = 0; cnt2 < objectOptionsArray.length(); cnt2++) {

                        final JSONObject s = objectOptionsArray.getJSONObject(cnt2);

                        final String entryTitle = s.getString("title");
                        final String entrySummary = s.getString("summary");
                        final boolean entryDefault = s.optBoolean("default", false);

                        s.remove("title");
                        s.remove("summary");
                        s.remove("default");

                        //

                        final MapListEntry mapListEntry = new MapListEntry(entryTitle, entrySummary);

                        //

                        final JSONArray sArray = s.names();

                        if (sArray != null && sArray.length() > 0) {

                            final String key = sArray.getString(0);

                            mapListEntry.setKey(key);
                            mapListEntry.setValue(s.getString(key));
                        }

                        mapListEntry.setChecked(entryDefault && !haveDefault);
                        mapListEntry.setDefault(entryDefault);
                        if (entryDefault)
                            haveDefault = true;

                        // /

                        mapListEntryList.add(mapListEntry);
                    }

                    if (!haveDefault && mapListEntryList.size() > 0) {
                        final MapListEntry first = mapListEntryList.get(0);
                        first.setChecked(true); // set first if we had no default
                        first.setDefault(true);
                    }

                    final MapListSection mapListSection = new MapListSection(sectionTitle, mapListEntryList);
                    mapFilterListSectionList.add(mapListSection);
                }
            }

            // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            // map type
            final MapListEntry entry = mapListSectionList.get(0).getMapListEntryList().get(0);

            activity.setCurrentMapType(entry);

            activity.setMapTypeListSectionList(mapListSectionList);
            activity.setMapFilterListSectionListMap(mapFilterListSectionListHash);

        } catch (final JSONException e) {
            e.printStackTrace();
        }

    } else
        Log.i(DEBUG_TAG, "LEERE LISTE");

    if (endTaskListener != null) {
        final JSONArray array = new JSONArray();
        array.put(result);
        endTaskListener.taskEnded(array);
    }
}

From source file:com.facebook.internal.ShareInternalUtilityTest.java

private boolean simpleJsonObjComparer(JSONObject obj1, JSONObject obj2) {
    if (obj1.names().length() != obj2.names().length()) {
        return false;
    }/*from   w  w w.j  a va2  s.  c o m*/

    Iterator<String> keys = obj1.keys();
    while (keys.hasNext()) {
        try {
            String key = keys.next();
            Object value1 = obj1.get(key);
            Object value2 = obj2.get(key);
            if (!jsonObjectValueComparer(value1, value2)) {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    return true;
}

From source file:fr.haploid.webservices.WebServicesHelper.java

protected static synchronized WebServicesResponseData downloadFromServer(String url, JSONObject params,
        String type) {/*from  w w w. j a  v a2s  .c o m*/

    int responseCode = 9999;

    StringBuffer buffer = new StringBuffer();

    Uri.Builder builder = new Uri.Builder();
    builder.encodedPath(url);

    JSONArray namesOfParams = params.names();

    for (int i = 0; i < namesOfParams.length(); i++) {
        try {
            String param = namesOfParams.getString(i);
            builder.appendQueryParameter(param, params.get(param).toString());
        } catch (JSONException e) {
            return new WebServicesResponseData("JSONException " + e.toString(), responseCode, false);
        }
    }

    URL urlBuild;

    try {
        urlBuild = new URL(builder.build().toString());
    } catch (MalformedURLException e) {
        return new WebServicesResponseData("MalformedURLException " + e.toString(), responseCode, false);
    }

    try {
        HttpURLConnection urlConnection = (HttpURLConnection) urlBuild.openConnection();
        try {
            urlConnection.setRequestMethod(type);
        } catch (ProtocolException e) {
            return new WebServicesResponseData("ProtocolException " + e.toString(), responseCode, false);
        }

        urlConnection.connect();
        responseCode = urlConnection.getResponseCode();
        if (responseCode < 400) {
            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream == null) {
                return new WebServicesResponseData(null, responseCode, false);
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                return new WebServicesResponseData(null, responseCode, false);
            }
        }
    } catch (IOException e) {
        return new WebServicesResponseData("IOException " + e.toString(), responseCode, false);
    }
    return new WebServicesResponseData(buffer.toString(), responseCode, true);
}

From source file:com.clarkparsia.pelletserver.client.utils.PelletServerUtils.java

/**
 * Parse all the {@link PelletService}s in the {@link PelletServer} root
 * //from  ww  w  .j  av  a  2 s.c  om
 * @param server
 *            The {@link PelletServer}
 * @param content
 *            The content of the HTTP request
 * @return the list of services
 * @throws PelletClientException
 *             if there was an error during invocation
 */
public static Collection<PelletService> parseServerRootForServices(PelletServer server, String content)
        throws PelletClientException {
    List<PelletService> services = Lists.newArrayList();

    try {
        JSONObject root = new JSONObject(content);
        JSONArray rnames = root.names();

        for (int i = 0; i < rnames.length(); i++) {

            String serviceName = rnames.getString(i);

            if (!serviceName.equals("knowledge-bases") && !serviceName.equals("server-information")) {
                PelletService service = parsePelletService(server, root.getJSONObject(serviceName),
                        serviceName);
                if (service != null)
                    services.add(service);
            }
        }

    } catch (Exception e) {
        throw new PelletClientException("Problem parsing services from server", e);
    }

    return services;
}

From source file:com.clarkparsia.pelletserver.client.utils.PelletServerUtils.java

/**
 * Parse {@link PelletServer} server-information
 * //from w w w. ja va  2 s .c o m
 * @param server
 *            the PelletServer instance
 * @param content
 *            the info from the server to be parsed
 * @return the parsed information
 * @throws PelletClientException
 *             if there was an error during invocation
 */
public static Map<String, String> parseServerInfo(PelletServer server, String content)
        throws PelletClientException {
    try {
        JSONObject root = new JSONObject(content);

        Map<String, String> info = Maps.newHashMap();

        if (root.has("server-information")) {
            JSONObject obj = root.getJSONObject("server-information");
            JSONArray names = obj.names();

            for (int i = 0; i < names.length(); i++) {
                String key = names.getString(i);
                String value = obj.getString(key);
                info.put(key, value);
            }
        }

        return info;
    } catch (Exception e) {
        throw new PelletClientException("Problem parsing server information", e);
    }
}

From source file:com.clarkparsia.pelletserver.client.utils.PelletServerUtils.java

/**
 * Parse a given {@link JSONObject} referent to a {@link KnowledgeBase}
 *///from   w  w w . ja  va  2 s .  com
private static KnowledgeBase parseKnowledgeBase(PelletServer server, JSONObject kb)
        throws JSONException, SecurityException, IllegalArgumentException, MalformedURLException,
        NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException,
        MimeTypeParseException, PelletClientException {
    String name = kb.getString("name");

    log.fine("Parsing kb " + name);

    KnowledgeBaseImpl knowledgeBase = new KnowledgeBaseImpl(server, null, name);

    JSONObject services = kb.getJSONObject("kb-services");

    JSONArray snames = services.names();
    for (int i = 0; i < snames.length(); i++) {

        String serviceName = snames.getString(i);
        JSONObject service = services.getJSONObject(serviceName);

        KBPelletService kbservice = parseKBPelletService(knowledgeBase, service, serviceName);

        if (kbservice != null) {
            knowledgeBase.addService(kbservice);
        }
    }

    return knowledgeBase;
}

From source file:pl.poznan.put.cs.ify.api.group.YCommData.java

public static YCommData fromJsonObject(JSONObject json) {
    try {/*  w ww. j a v a 2s.  c om*/
        YCommData data = new YCommData();
        JSONObject user = json.getJSONObject(USER);
        data.mUserData = YUserData.fromJsonObject(user);
        JSONObject event = json.getJSONObject(EVENT);
        data.mEventTag = event.getInt(TAG);
        data.mEventTarget = event.getString(TARGET);
        JSONObject values = json.getJSONObject(VALUES);
        if (values != null) {
            JSONArray names = values.names();
            YLog.i("YCommData", "Values");
            if (names != null) {
                for (int i = 0; i < names.length(); i++) {
                    String name = names.getString(i);
                    JSONObject val = values.getJSONObject(name);
                    data.mValues.put(name, YParam.fromJsonObj(val));
                }

            }
        }
        YLog.i("YCommData", "Values filled");
        return data;

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/**
 * Load registry from JSON.//ww w  .  j  a  va  2 s  . co  m
 */
public void load(String json) {
    mChanged = false;
    mModels = new HashMap<Long, JSONObject>();
    try {
        JSONObject modelarray = new JSONObject(json);
        JSONArray ids = modelarray.names();
        if (ids != null) {
            for (int i = 0; i < ids.length(); i++) {
                String id = ids.getString(i);
                JSONObject o = modelarray.getJSONObject(id);
                mModels.put(o.getLong("id"), o);
            }
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jennifer.ui.chart.ChartBuilder.java

@Override
public void drawBefore() {

    JSONObject series = cloneObject("series");
    JSONObject grid = cloneObject("grid");
    Object brush = clone("brush");
    Object widget = clone("widget");

    JSONArray data = cloneArray("data");

    // series ?? 
    for (int i = 0, len = data.length(); i < len; i++) {
        JSONObject row = (JSONObject) data.getJSONObject(i);

        Iterator it = row.keys();

        while (it.hasNext()) {
            String key = (String) it.next();

            if (!series.has(key)) {
                series.put(key, new JSONObject());
            }/*from w w w .  jav a  2s.  co  m*/

            JSONObject obj = JSONUtil.clone(series.getJSONObject(key));

            if (obj == null)
                continue;

            Object valueObject = row.get(key);

            if (valueObject instanceof String)
                continue;

            if (valueObject instanceof Double || valueObject instanceof Integer) {
                double value = row.getDouble(key);

                if (!obj.has("min"))
                    obj.put("min", value);
                if (!obj.has("max"))
                    obj.put("max", value);

                if (value < obj.getDouble("min"))
                    obj.put("min", value);
                if (value > obj.getDouble("max"))
                    obj.put("max", value);
            } else if (row.get(key) instanceof Long) {
                long value = row.getLong(key);

                if (!obj.has("min"))
                    obj.put("min", value);
                if (!obj.has("max"))
                    obj.put("max", value);

                if (value < obj.getLong("min"))
                    obj.put("min", value);
                if (value > obj.getLong("max"))
                    obj.put("max", value);
            }

            series.put(key, obj);
        }
    }
    // series_list
    barray("brush", createBrushData(brush, series.names()));
    barray("widget", createBrushData(widget, series.names()));
    bobject("grid", grid);
    bobject("hash", null);
    barray("data", data);
    bobject("series", series);

}