Example usage for org.json JSONArray put

List of usage examples for org.json JSONArray put

Introduction

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

Prototype

public JSONArray put(Object value) 

Source Link

Document

Append an object value.

Usage

From source file:io.github.minime89.passbeam.keyboard.Layouts.java

public JSONObject dump() throws JSONException {
    JSONObject obj = new JSONObject();

    JSONArray layoutsArr = new JSONArray();
    for (Layout layout : layouts) {
        layoutsArr.put(layout.dump());
    }// w w w .ja  v  a  2 s . com
    obj.put("layouts", layoutsArr);

    return obj;
}

From source file:org.chromium.ChromeI18n.java

private void getAcceptLanguages(final CordovaArgs args, final CallbackContext callbackContext) {
    try {/*from   ww  w  .j  av a 2s  .  c om*/
        JSONArray ret = new JSONArray();
        Locale locale = Locale.getDefault();
        String localString = locale.toString().replace('_', '-');
        ret.put(localString);
        callbackContext.success(ret);
    } catch (Exception e) {
        callbackContext.error("Could not retrieve supported locales");
        Log.e(LOG_TAG, "Could not retrieve supported locales", e);
    }
}

From source file:com.marpies.ane.facebook.accountkit.functions.LoadPreferencesFunction.java

@Override
public FREObject call(FREContext context, FREObject[] args) {
    super.call(context, args);

    AIR.log("AccountKit::loadPreference");
    final int callbackId = FREObjectUtils.getInt(args[0]);

    /* User is not logged in, cannot load preferences */
    if (AccountKit.getCurrentAccessToken() == null) {
        dispatchError(callbackId, "User is not logged in, cannot load preferences.");
        return null;
    }//ww w .j  av  a2 s  . c  o  m

    AccountKit.getAccountPreferences().loadPreferences(new AccountPreferences.OnLoadPreferencesListener() {
        @Override
        public void onLoadPreferences(@Nullable Map<String, String> prefs,
                @Nullable AccountKitError accountKitError) {
            if (accountKitError != null) {
                dispatchError(callbackId, accountKitError.getErrorType().getMessage());
            } else {
                AIR.log("AccountKit | successfully loaded preferences");
                JSONObject response = new JSONObject();
                JSONUtils.addToJSON(response, "callbackId", callbackId);
                JSONArray preferences = new JSONArray();
                for (Map.Entry<String, String> preference : prefs.entrySet()) {
                    preferences.put(preference.getKey());
                    preferences.put(preference.getValue());
                }
                JSONUtils.addToJSON(response, "preferences", preferences);
                AIR.dispatchEvent(AccountKitEvent.LOAD_PREFERENCES, response.toString());
            }
        }
    });

    return null;
}

From source file:charitypledge.Pledge.java

public void JsonWrite(String[] args) {

    String[] values = args;//from w  w  w. j  a  va2  s .  co m
    JSONObject obj = new JSONObject();
    JSONArray objArray = new JSONArray();
    try {
        if (args == null || values.length == 0) {
            throw new Exception("Noting to write to file");
        } else {
            String title = "";
            String value = "";

            for (int i = (values.length - 1); i >= 0; i--) {
                if ((i % 2) == 0) {
                    title = values[i];
                    obj.put(title, value);
                } else {
                    value = values[i];
                }
            }
            objArray.put(obj);
        }

        try {
            try {
                InputStream foo = new FileInputStream(JSONFile);
                JSONTokener t = new JSONTokener(foo);
                JSONObject json = new JSONObject(t);
                foo.close();
                FileWriter file = new FileWriter(JSONFile);
                json.append("contributors", obj);
                file.write(json.toString(5));
                file.close();
                tableRefresh();
            } catch (FileNotFoundException e) {
                JSONWriter jsonWriter;
                try {
                    jsonWriter = new JSONWriter(new FileWriter(JSONFile));
                    jsonWriter.object();
                    jsonWriter.key("contributors");
                    jsonWriter.array();
                    jsonWriter.endArray();
                    jsonWriter.endObject();
                    InputStream foo = new FileInputStream(JSONFile);
                    JSONTokener t = new JSONTokener(foo);
                    JSONObject json = new JSONObject(t);
                    foo.close();
                    FileWriter file = new FileWriter(JSONFile);
                    json.append("contributors", obj);
                    file.write(json.toString(5));
                    file.close();
                    tableRefresh();
                } catch (IOException f) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        int pic = JOptionPane.ERROR_MESSAGE;
        JOptionPane.showMessageDialog(null, e, "", pic);
    }
}

From source file:org.chromium.ChromeSystemNetwork.java

private void getNetworkInterfaces(final CordovaArgs args, final CallbackContext callbackContext) {
    cordova.getThreadPool().execute(new Runnable() {
        @Override/*from  w w  w .ja v a2s.  c o  m*/
        public void run() {
            try {
                JSONArray ret = new JSONArray();
                ArrayList<NetworkInterface> interfaces = Collections
                        .list(NetworkInterface.getNetworkInterfaces());
                for (NetworkInterface iface : interfaces) {
                    if (iface.isLoopback()) {
                        continue;
                    }
                    for (InterfaceAddress interfaceAddress : iface.getInterfaceAddresses()) {
                        InetAddress address = interfaceAddress.getAddress();
                        if (address == null) {
                            continue;
                        }
                        JSONObject data = new JSONObject();
                        data.put("name", iface.getDisplayName());
                        // Strip address scope zones for IPv6 address.
                        data.put("address", address.getHostAddress().replaceAll("%.*", ""));
                        data.put("prefixLength", interfaceAddress.getNetworkPrefixLength());

                        ret.put(data);
                    }
                }

                callbackContext.success(ret);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Error occured while getting network interfaces", e);
                callbackContext.error("Could not get network interfaces");
            }
        }
    });
}

From source file:org.jbpm.designer.server.diagram.JSONBuilder.java

/**
 * Parses all child Shapes recursively and adds them to the correct JSON
 * Object//from ww w .  j a va2s .c om
 * 
 * @param childShapes
 * @throws org.json.JSONException
 */
private static JSONArray parseChildShapesRecursive(ArrayList<Shape> childShapes) throws JSONException {
    if (childShapes != null) {
        JSONArray childShapesArray = new JSONArray();

        for (Shape childShape : childShapes) {
            JSONObject childShapeObject = new JSONObject();

            childShapeObject.put("resourceId", childShape.getResourceId().toString());
            childShapeObject.put("properties", parseProperties(childShape.getProperties()));
            childShapeObject.put("stencil", parseStencil(childShape.getStencilId()));
            childShapeObject.put("childShapes", parseChildShapesRecursive(childShape.getChildShapes()));
            childShapeObject.put("outgoing", parseOutgoings(childShape.getOutgoings()));
            childShapeObject.put("bounds", parseBounds(childShape.getBounds()));
            childShapeObject.put("dockers", parseDockers(childShape.getDockers()));

            if (childShape.getTarget() != null)
                childShapeObject.put("target", parseTarget(childShape.getTarget()));

            childShapesArray.put(childShapeObject);
        }

        return childShapesArray;
    }

    return new JSONArray();
}

From source file:org.jbpm.designer.server.diagram.JSONBuilder.java

/**
 * Delivers the correct JSON Object for the dockers
 * /*from w w w  . java 2  s.  c  o  m*/
 * @param dockers
 * @throws org.json.JSONException
 */
private static JSONArray parseDockers(ArrayList<Point> dockers) throws JSONException {
    if (dockers != null) {
        JSONArray dockersArray = new JSONArray();

        for (Point docker : dockers) {
            JSONObject dockerObject = new JSONObject();

            dockerObject.put("x", docker.getX().doubleValue());
            dockerObject.put("y", docker.getY().doubleValue());

            dockersArray.put(dockerObject);
        }

        return dockersArray;
    }

    return new JSONArray();
}

From source file:org.jbpm.designer.server.diagram.JSONBuilder.java

/**
 * Delivers the correct JSON Object for outgoings
 * /*from w ww  .  j  av  a2s.  co m*/
 * @param outgoings
 * @throws org.json.JSONException
 */
private static JSONArray parseOutgoings(ArrayList<Shape> outgoings) throws JSONException {
    if (outgoings != null) {
        JSONArray outgoingsArray = new JSONArray();

        for (Shape outgoing : outgoings) {
            JSONObject outgoingObject = new JSONObject();

            outgoingObject.put("resourceId", outgoing.getResourceId().toString());
            outgoingsArray.put(outgoingObject);
        }

        return outgoingsArray;
    }

    return new JSONArray();
}

From source file:org.jbpm.designer.server.diagram.JSONBuilder.java

/**
 * Delivers the correct JSON Object for the Stencilset Extensions
 * /*from  w w  w  .  j av a  2  s . c  o m*/
 * @param extensions
 */
private static JSONArray parseStencilSetExtensions(ArrayList<String> extensions) {
    if (extensions != null) {
        JSONArray extensionsArray = new JSONArray();

        for (String extension : extensions)
            extensionsArray.put(extension.toString());

        return extensionsArray;
    }

    return new JSONArray();
}

From source file:de.joinout.criztovyl.tools.json.JSONCollection.java

/**
 * Sets up a new JSON collection helper.
 * @param coll the {@link Collection}//www . ja v a  2 s.co  m
 * @param creator the {@link JSONCreator} for the generic class.
 */
public JSONCollection(Collection<T> coll, JSONCreator<T> creator) {
    this(creator);

    JSONArray array = new JSONArray();

    for (T t : coll)
        array.put(creator.canBeString() ? creator.string(t) : creator.getJSON(t));

    getJSON().put(getKey(), array);
}