Example usage for org.json.simple JSONArray add

List of usage examples for org.json.simple JSONArray add

Introduction

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

Prototype

public boolean add(E e) 

Source Link

Document

Appends the specified element to the end of this list.

Usage

From source file:dependencies.DependencyResolving.java

/**
 * @param args the command line arguments
 *//*w w  w  .  j  av a 2s.  c  o  m*/
public static void main(String[] args) {
    // TODO code application logic here
    JSONParser parser = new JSONParser(); //we use JSONParser in order to be able to read from JSON file
    try { //here we declare the file reader and define the path to the file dependencies.json
        Object obj = parser.parse(new FileReader(
                "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\dependencies.json"));
        JSONObject project = (JSONObject) obj; //a JSON object containing all the data in the .json file
        JSONArray dependencies = (JSONArray) project.get("dependencies"); //get array of objects with key "dependencies"
        System.out.print("We need to install the following dependencies: ");
        Iterator<String> iterator = dependencies.iterator(); //define an iterator over the array "dependencies"
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        } //on the next line we declare another object, which parses a Parser object and reads from all_packages.json
        Object obj2 = parser.parse(new FileReader(
                "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\all_packages.json"));
        JSONObject tools = (JSONObject) obj2; //a JSON object containing all thr data in the file all_packages.json
        for (int i = 0; i < dependencies.size(); i++) {
            if (tools.containsKey(dependencies.get(i))) {
                System.out.println(
                        "In order to install " + dependencies.get(i) + ", we need the following programs:");
                JSONArray temporaryArray = (JSONArray) tools.get(dependencies.get(i)); //a temporary JSON array in which we store the keys and values of the dependencies
                for (i = 0; i < temporaryArray.size(); i++) {
                    System.out.println(temporaryArray.get(i));
                }
                ArrayList<Object> arraysOfJsonData = new ArrayList<Object>(); //an array in which we will store the keys of the objects, after we use the values and won't need them anymore
                for (i = 0; i < temporaryArray.size(); i++) {
                    System.out.println("Installing " + temporaryArray.get(i));
                }
                while (!temporaryArray.isEmpty()) {

                    for (Object element : temporaryArray) {

                        if (tools.containsKey(element)) {
                            JSONArray secondaryArray = (JSONArray) tools.get(element); //a temporary array within the scope of the if-statement
                            if (secondaryArray.size() != 0) {
                                System.out.println("In order to install " + element + ", we need ");
                            }
                            for (i = 0; i < secondaryArray.size(); i++) {
                                System.out.println(secondaryArray.get(i));
                            }

                            for (Object o : secondaryArray) {

                                arraysOfJsonData.add(o);
                                //here we create a file with the installed dependency
                                File file = new File(
                                        "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\installed_modules\\"
                                                + o);
                                if (file.createNewFile()) {
                                    System.out.println(file.getName() + " is installed!");
                                } else {
                                }
                            }
                            secondaryArray.clear();
                        }
                    }
                    temporaryArray.clear();
                    for (i = 0; i < arraysOfJsonData.size(); i++) {
                        temporaryArray.add(arraysOfJsonData.get(i));
                    }
                    arraysOfJsonData.clear();
                }
            }
        }
        Set<String> keys = tools.keySet(); // here we define a set of keys of the objects in all_packages.json
        for (String s : keys) {
            File file = new File(
                    "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\installed_modules\\"
                            + s);
            if (file.createNewFile()) {
                System.out.println(file.getName() + " is installed.");
            } else {
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(DependencyResolving.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(DependencyResolving.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:de.ingrid.external.gemet.JSONUtils.java

public static JSONArray toJSONArray(JSONObject jsonObj) {
    JSONArray jsonArray = new JSONArray();
    jsonArray.add(jsonObj);

    return jsonArray;
}

From source file:me.prokopyl.storagemonitor.httpserver.HTTPResponse.java

static public String ToJSONString(Object object) {
    if (object instanceof JSONAware) {
        return ((JSONAware) object).toJSONString();
    } else {/*  ww w .j  a va  2  s. c  o m*/
        JSONArray array = new JSONArray();
        array.add(object);
        return array.toJSONString();
    }
}

From source file:naftoreiclag.villagefive.util.json.JSONUtil.java

public static <SomeJSONThingy extends JSONThingy> JSONArray encodePntrList(List<SomeJSONThingy> list) {
    JSONArray conv = new JSONArray();

    for (int i = 0; i < list.size(); ++i) {
        conv.add(list.get(i).getJsonIndex());
    }/*  www . j a v  a 2s  .  c  om*/

    return conv;
}

From source file:mysynopsis.JSONWriter.java

public static void writeData() throws IOException {

    JSONObject bio = new JSONObject();
    bio.put("Name", name);
    bio.put("Initial", initial);
    bio.put("Bio", biog);
    bio.put("Designation", designation);
    bio.put("Department", dept);
    bio.put("University", university);
    bio.put("University_Address", universityAddress);
    bio.put("Office", office);
    bio.put("Ohours", officehours);
    bio.put("Phone", phone);
    bio.put("Email", email);
    bio.put("Resume_Link", resumelink);
    bio.put("Educational", education);
    bio.put("Professional", professional);

    bio.put("Awards", awards);

    bio.put("Image_Ext", imgExtension);
    bio.put("Image_String", imgString);

    /*JSONArray education = new JSONArray();
    for(int i=0;i<8;i++) {/* w  w w  . j a  v a  2  s. co m*/
    education.add(educ[i]);
    }
    bio.put("Education",education);*/

    JSONArray ftpinfo = new JSONArray();

    ftpinfo.add(server);
    ftpinfo.add(user);
    ftpinfo.add(dir);

    bio.put("Server_Information", ftpinfo);

    try (FileWriter file = new FileWriter("data.json")) {

        file.write(bio.toJSONString());
    } catch (IOException e) {

        JOptionPane.showMessageDialog(null,
                "Can't Create Data.json File. Please Check Your Directory Permission");
    }
}

From source file:com.telefonica.iot.cygnus.utils.CommonUtilsForTests.java

/**
 * Creates a JSONObject-like notification.
 * @return A JSONObject-like notification
 *///from  ww  w . ja va 2s  .  co m
public static JSONObject createNotification() {
    JSONObject attribute = new JSONObject();
    attribute.put("name", "temperature");
    attribute.put("type", "centigrade");
    attribute.put("value", "26.5");
    JSONArray attributes = new JSONArray();
    attributes.add(attribute);
    JSONObject contextElement = new JSONObject();
    contextElement.put("attributes", attributes);
    contextElement.put("type", "Room");
    contextElement.put("isPattern", "false");
    contextElement.put("id", "room1");
    JSONObject statusCode = new JSONObject();
    statusCode.put("code", "200");
    statusCode.put("reasonPhrase", "OK");
    JSONObject contextResponse = new JSONObject();
    contextResponse.put("contextElement", contextElement);
    contextResponse.put("statusCode", statusCode);
    JSONArray contextResponses = new JSONArray();
    contextResponses.add(contextResponse);
    JSONObject notification = new JSONObject();
    notification.put("subscriptionId", "51c0ac9ed714fb3b37d7d5a8");
    notification.put("originator", "localhost");
    notification.put("contextResponses", contextResponses);
    return notification;
}

From source file:com.p000ison.dev.simpleclans2.exceptions.handling.ExceptionReport.java

private static Object buildThrowableJSON(Throwable thrown) {
    JSONArray stackTrace = new JSONArray();

    for (StackTraceElement element : thrown.getStackTrace()) {
        stackTrace.add(element.toString());
    }/*from   w  w w . j  a  v a2  s  .c  o m*/

    return stackTrace;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.DomainObjectJSONSerializer.java

private static JSONArray serializeRelation(Set<? extends AbstractDomainObject> roleSet) {
    JSONArray jsonList = new JSONArray();
    for (AbstractDomainObject obj : roleSet) {
        jsonList.add(obj.getExternalId());
    }/*from  w w w . j a  va2  s.c o m*/
    return jsonList;
}

From source file:app.NotificationJSONizer.java

public static void jsonize(ArrayList<backend.Notification> notis, PrintWriter out) {
    JSONArray a = new JSONArray();
    notis.stream().forEach((noti) -> {
        JSONObject o = noti.jsonize();/*  w w w. j  a  v a2 s  .c om*/
        o.put("type", noti.get_type().toString());
        a.add(o);
    });

    out.print(a.toString());
}

From source file:net.jselby.pc.player.ChatMessage.java

/**
 * Converts a string to a basic ChatMessage
 *
 * @param message The String message//from   www  . java 2s.  c  om
 * @return A Chat message
 */
public static ChatMessage convertToJson(String message) {
    JSONObject obj = new JSONObject();

    // Parse message, looking for colors etc.
    ArrayList<JSONObject> objects = new ArrayList<JSONObject>();

    ChatColor color = ChatColor.WHITE;
    String currentMessage = "";

    boolean nextByteAsColor = false;

    for (char bytes : message.toCharArray()) {
        if (nextByteAsColor) {
            color = ChatColor.getByChar(bytes);
            nextByteAsColor = false;
            continue;
        }
        if (bytes == ChatColor.COLOR_CHAR) { // Special color symbol
            if (currentMessage.length() > 0) {
                JSONObject currentMessageAsJSON = new JSONObject();
                currentMessageAsJSON.put("text", currentMessage);
                currentMessageAsJSON.put("color", color.name().toLowerCase());
                objects.add(currentMessageAsJSON);
            }
            color = ChatColor.WHITE;
            currentMessage = "";
            nextByteAsColor = true;
        } else {
            currentMessage += bytes;
        }
    }

    if (currentMessage.length() > 0) {
        JSONObject rest = new JSONObject();
        rest.put("text", currentMessage);
        rest.put("color", color.name().toLowerCase());
        objects.add(rest);
    }

    JSONArray array = new JSONArray();

    for (JSONObject jsonObject : objects) {
        array.add(jsonObject);
    }

    obj.put("extra", array);
    obj.put("text", "");

    return new ChatMessage(obj);
}