Example usage for org.json JSONObject getNames

List of usage examples for org.json JSONObject getNames

Introduction

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

Prototype

public static String[] getNames(Object object) 

Source Link

Document

Get an array of field names from an Object.

Usage

From source file:org.eclipse.flux.client.RequestResponseHandler.java

protected JSONObject copy(JSONObject req) {
    try {/* w w w . j ava 2s .co  m*/
        JSONObject res = new JSONObject(req);
        for (String name : JSONObject.getNames(req)) {
            res.put(name, req.get(name));
        }
        return res;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

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

public static double parseDouble(String key, JSONObject series) {

    // setting/*from w ww . j  a  v a 2 s  . com*/
    String[] names = JSONObject.getNames(series);
    for (String name : names) {
        if (series.getJSONObject(name).has("max")) {
            key = key.replace("{" + name + "}", series.getJSONObject(name).getDouble("max") + "");
        }
    }

    // caculate
    String[] list = key.split(" ");

    double value = Double.parseDouble(list[0]);

    for (int i = 1; i < list.length; i += 2) {
        String operator = list[i];

        if (operator.startsWith("+")) {
            value += Double.parseDouble(list[i + 1]);
        } else if (operator.startsWith("/")) {
            value /= Double.parseDouble(list[i + 1]);
        } else if (operator.startsWith("-")) {
            value -= Double.parseDouble(list[i + 1]);
        } else if (operator.startsWith("*")) {
            value *= Double.parseDouble(list[i + 1]);
        }
    }

    return value;
}

From source file:juserdefaults.JUserDefaults.java

public Map<String, Object> getMap(String key) {
    JSONObject jsonObject = getKeyValueStore().getJSONObject(key);

    Map<String, Object> map = new HashMap<String, Object>();

    String[] keys = JSONObject.getNames(jsonObject);
    for (int i = 0; i < keys.length; i++) {
        String k = keys[i];//ww  w .  ja v a2 s .  c om
        Object v = jsonObject.get(k);
        map.put(k, v);
    }
    return map;
}

From source file:com.thoughtworks.xstream.io.json.JsonWriterModeTest.java

private static boolean equals(JSONObject object1, JSONObject object2) {
    String[] names = JSONObject.getNames(object1);
    try {//from ww  w  .ja  va  2  s .  c o  m
        if (names == null) {
            return JSONObject.getNames(object2) == null;
        }
        if (new HashSet(Arrays.asList(names))
                .equals(new HashSet(Arrays.asList(JSONObject.getNames(object2))))) {
            for (int i = 0; i < names.length; i++) {
                if (!equals(object1.get(names[i]), object2.get(names[i]))) {
                    return false;
                }
            }
            return true;
        }
    } catch (JSONException e) {
        // ignore - return false
    }
    return false;
}

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

public static JSONObject clone(JSONObject obj) {

    if (obj == null)
        return null;
    JSONObject o = new JSONObject();

    String[] names = JSONObject.getNames(obj);

    if (names == null)
        return null;

    for (String name : names) {
        o.put(name, obj.get(name));//from   ww w.j  a v  a2s .co m
    }

    return o;
}

From source file:edu.umass.cs.gigapaxos.paxospackets.PaxosPacket.java

public JSONObject toJSONObject() throws JSONException {
    JSONObject json = new JSONObject();
    // tells Packet that this is a PaxosPacket
    JSONPacket.putPacketType(json, PaxosPacket.PaxosPacketType.PAXOS_PACKET.getInt());
    // the specific type of PaxosPacket
    json.put(PaxosPacket.Keys.PT.toString(), this.packetType.getInt());
    json.put(PaxosPacket.Keys.ID.toString(), this.paxosID);
    json.put(PaxosPacket.Keys.V.toString(), this.version);

    // copy over child fields
    JSONObject child = toJSONObjectImpl();
    for (String name : JSONObject.getNames(child))
        json.put(name, child.get(name));

    return json;// w  w  w .  ja  v a2s .  c  om
}

From source file:org.eclipse.flux.client.config.RabbitMQFluxConfig.java

public static String rabbitUrl() throws Exception {
    String vcapServices = System.getenv("VCAP_SERVICES");
    if (vcapServices != null) {
        JSONObject svcInfo = JSON.parse(vcapServices);
        console.log("VCAP_SERVICES = " + svcInfo);
        for (String label : JSONObject.getNames(svcInfo)) {
            JSONArray svcs = svcInfo.getJSONArray(label);
            for (int index = 0; index < svcs.length(); index++) {
                String uri = svcs.getJSONObject(index).getJSONObject("credentials").getString("uri");
                if (uri.contains("amqp")) {
                    console.log("rabbit url = " + uri);
                    return uri;
                }//www  .  j ava  2  s  . c o  m
            }
        }
        throw new Error("Running on CF requires that you bind a amqp service to this app");
    } else {
        return "amqp://localhost";
    }
}

From source file:org.wso2.carbon.dataservices.core.description.query.QueryFactory.java

private static void addJSONMappingResultRecord(JSONObject obj, OMElement parentEl) throws DataServiceFault {
    Object item;/* w ww .  j a v  a2  s . c om*/
    for (String name : JSONObject.getNames(obj)) {
        try {
            item = obj.get(name);
        } catch (JSONException e) {
            throw new DataServiceFault(e, "Unexpected JSON parsing error: " + e.getMessage());
        }
        if (name.startsWith("@")) {
            processJSONMappingCallQuery(name.substring(1), item, parentEl);
        } else {
            processJSONMappingResultColumn(name, item, parentEl);
        }
    }
}

From source file:org.wso2.carbon.dataservices.core.description.query.QueryFactory.java

public static OMElement getJSONResultFromText(String jsonMapping) throws DataServiceFault {
    try {//from  www  . j a  va  2  s  .c o m
        OMElement resultEl = createElement(DBSFields.RESULT);
        JSONObject resultObj = new JSONObject(jsonMapping);
        String[] topLevelNames = JSONObject.getNames(resultObj);
        if (topLevelNames == null || topLevelNames.length != 1) {
            throw new DataServiceFault("There must be exactly 1 top level object in the JSON mapping, "
                    + "found " + (topLevelNames != null ? topLevelNames.length : 0));
        }
        String wrapperName = topLevelNames[0];
        String rowName = null;
        Object rootObj = resultObj.get(wrapperName);
        if (rootObj instanceof JSONArray) {
            throw new DataServiceFault("The top level object cannot be an array");
        } else if (rootObj instanceof JSONObject) {
            String[] secondLevelNames = JSONObject.getNames((JSONObject) rootObj);
            if (secondLevelNames.length == 1) {
                Object obj = ((JSONObject) rootObj).get(secondLevelNames[0]);
                if (obj instanceof JSONArray) {
                    rowName = secondLevelNames[0];
                    JSONArray array = (JSONArray) obj;
                    if (array.length() != 1) {
                        throw new DataServiceFault(
                                "The JSON array should be of size 1, found " + array.length());
                    }
                    obj = array.get(0);
                    if (!(obj instanceof JSONObject)) {
                        throw new DataServiceFault("The JSON array element must be a JSON Object");
                    }
                    addJSONMappingResultRecord((JSONObject) obj, resultEl);
                } else {
                    addJSONMappingResultRecord((JSONObject) rootObj, resultEl);
                }
            } else {
                addJSONMappingResultRecord((JSONObject) rootObj, resultEl);
            }
        } else {
            throw new DataServiceFault("The top level object cannot be a simple type");
        }
        resultEl.addAttribute(DBSFields.ELEMENT, wrapperName, null);
        if (rowName != null) {
            resultEl.addAttribute(DBSFields.ROW_NAME, rowName, null);
        }
        return resultEl;
    } catch (DataServiceFault e) {
        throw e;
    } catch (Exception e) {
        throw new DataServiceFault(e, "Error in parsing JSON result mapping: " + e.getMessage());
    }
}

From source file:cc.osint.graphd.processes.TestGraphProcess.java

public void message(JSONObject msg) {
    try {//from   w w  w  .  jav a2 s.c  om
        JSONObject visited;
        if (!msg.has("visited")) {
            visited = new JSONObject();
        } else {
            visited = msg.getJSONObject("visited");
            if (visited.has(getContext().getString(Graph.KEY_FIELD))) {
                return;
            }
        }
        visited.put(getContext().getString(Graph.KEY_FIELD), System.nanoTime());
        msg.put("visited", visited);

        //log(getContext().getString(Graph.KEY_FIELD) + ": msg = " + msg);
        int maxLen = 9999999;
        if (msg.has("maxlen")) {
            maxLen = msg.getInt("maxlen");
        }

        int msgsSent = 0;
        if (JSONObject.getNames(visited).length < maxLen) {
            // spread like wildfire!
            Set<String> rels = null;
            if (msg.has("via")) {
                rels = new HashSet<String>();
                rels.add(msg.getString("via"));
            }
            Set<JSONVertex> neighbors = getGraph().getOutgoingNeighborsOf(getContext(), rels);
            for (JSONVertex neighbor : neighbors) {
                String neighborKey = neighbor.getString(Graph.KEY_FIELD);
                if (visited.has(neighborKey)) {
                    //log(">> already visited " + neighborKey + " at " + visited.getString(neighborKey));
                } else {
                    emit(neighborKey, "testGraphProcess", new JSONObject(msg.toString()));
                    msgsSent++;
                }
            }
        }

        if (msgsSent == 0) {
            JSONObject visitObj = msg.getJSONObject("visited");
            String[] pathKeys = JSONObject.getNames(visitObj);
            ;
            String[] pathKeys1 = new String[pathKeys.length];
            for (int i = 0; i < pathKeys.length; i++) {
                pathKeys1[i] = visitObj.getString(pathKeys[i]) + "|" + pathKeys[i];
            }
            Arrays.sort(pathKeys1);
            String p = "";
            for (String pk : pathKeys1) {
                String pk1 = pk.substring(pk.indexOf("|") + 1);
                p += " -> " + pk1;
            }
            log("ENDPOINT: " + p);
        } else {
            //log(getContext().getKey() + " -> " + msgsSent);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        try {
            log(getContext().getString(Graph.KEY_FIELD) + ": exception: " + ex.getMessage());
        } catch (Exception ex1) {
            ex1.printStackTrace();
        }
    }
}