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.chart.ChartBuilder.java

private void drawGrid() {
    JSONObject grid = builderoptions.getJSONObject("grid");

    String[] names = JSONObject.getNames(grid);
    if (names == null)
        return;/*from  w  w w .jav a  2  s.co  m*/

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

        // create default cusotm grid
        if (grid.has("type")) {
            grid = new JSONObject().put("c", new JSONArray().put(JSONUtil.clone(grid)));
        }

        if (!builderoptions.has("scales")) {
            builderoptions.put("scales", new JSONObject());
        }

        JSONObject scales = (JSONObject) builderoptions.getJSONObject("scales");

        JSONArray keys = grid.names();

        for (int i = 0, len = keys.length(); i < len; i++) {
            String key = keys.getString(i);
            Orient orient = Orient.CUSTOM;

            if ("x".equals(key)) {
                orient = Orient.BOTTOM;
            } else if ("y".equals(key)) {
                orient = Orient.LEFT;
            } else if ("x1".equals(key)) {
                orient = Orient.TOP;
            } else if ("y1".equals(key)) {
                orient = Orient.RIGHT;
            }

            if (!scales.has(key)) {
                scales.put(key, new JSONArray());
            }

            JSONArray scale = (JSONArray) scales.getJSONArray(key);

            Object objGrid = grid.get(key);

            if (!(objGrid instanceof JSONArray) && !(objGrid instanceof JSONArray)) {
                JSONArray o = new JSONArray();
                o.put(JSONUtil.clone(grid.getJSONObject(key)));

                grid.put(key, o);
            } else if (objGrid instanceof JSONArray) {
                grid.put(key, JSONUtil.clone((JSONArray) objGrid));
            }

            JSONArray gridObject = (JSONArray) grid.getJSONArray(key);

            for (int keyIndex = 0, gridLen = gridObject.length(); keyIndex < gridLen; keyIndex++) {

                JSONObject g = JSONUtil.clone(gridObject.getJSONObject(keyIndex));

                Class cls = grids.get(g.getString("type"));
                Grid newGrid = null;

                try {
                    newGrid = (Grid) cls
                            .getDeclaredConstructor(Orient.class, ChartBuilder.class, JSONObject.class)
                            .newInstance(orient, this, g);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

                JSONObject ret = (JSONObject) newGrid.render();

                int dist = g.optInt("dist", 0);
                Transform root = (Transform) ret.get("root");

                if ("y".equals(key)) {
                    root.translate(area("x") - dist, area("y"));
                } else if ("y1".equals(key)) {
                    root.translate(area("x2") + dist, area("y"));
                } else if ("x".equals(key)) {
                    root.translate(area("x"), area("y2") + dist);
                } else if ("x1".equals(key)) {
                    root.translate(area("x"), area("y") - dist);
                }

                this.root.append(root);

                scales.getJSONArray(key).put(keyIndex, newGrid);

            }

        }
    }
}

From source file:org.envirocar.app.json.StreamTrackEncoder.java

private JsonObject createTrackProperties(Track track, String trackSensorName) throws JSONException {
    JsonObject result = new JsonObject();

    result.addProperty("sensor", trackSensorName);
    result.addProperty("description", track.getDescription());
    result.addProperty("name", track.getName());

    if (track.getMetadata() != null) {
        JSONObject json = track.getMetadata().toJson();
        JSONArray names = json.names();
        for (int i = 0; i < names.length(); i++) {
            result.addProperty(names.get(i).toString(), json.getString(names.get(i).toString()));
        }// w  w w. j av  a2s.  c om
    }

    return result;
}