Example usage for org.json.simple JSONObject JSONObject

List of usage examples for org.json.simple JSONObject JSONObject

Introduction

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

Prototype

JSONObject

Source Link

Usage

From source file:com.diskoverorta.utils.JsonConvertor.java

public String JsonConvertor() {

    PersonEntity p = new PersonEntity();
    PercentEntity pe = new PercentEntity();
    DateEntity d = new DateEntity();
    TimeEntity t = new TimeEntity();
    CurrencyEntity m = new CurrencyEntity();
    LocationEntity l = new LocationEntity();
    OrganizationEntity o = new OrganizationEntity();

    JSONObject obj = new JSONObject();
    /*/* w w w .  j  a  v a2s  . c o m*/
            obj.put("PERSON", p.Person().replace("\n", ", "));
            obj.put("Percent", pe.Percent().replace("\n", ", "));
            obj.put("Date", d.Date().replace("\n", ", "));
            obj.put("Time", t.Time().replace("\n", ", "));
            obj.put("Money", m.Money().replace("\n", ", "));
            obj.put("Location", l.Location().replace("\n", ", "));
            obj.put("Organization", o.Organization().replace("\n", ", "));
    */
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonOutput = gson.toJson(obj);
    System.out.print(jsonOutput);

    return jsonOutput;
}

From source file:com.intuit.tweetstest.TweetsController.java

@RequestMapping(value = "/feed", method = RequestMethod.GET)
protected String fetchFeeds(HttpServletRequest request, HttpServletResponse response)
        throws ParseException, ServletException, IOException, Exception {

    JSONObject result = new JSONObject();

    String user = request.getParameter("user");
    if (user != null && !user.isEmpty()) {
        String countStr = request.getParameter("count");
        int count;
        // If count parameter is not sent in request, set it to 100
        if (countStr != null && !countStr.isEmpty()) {
            count = Integer.parseInt(countStr);
        } else {/*from w  ww  .j av a 2 s  .co  m*/
            count = 100;
        }

        // The access to the MongoDB should itself be a part of a separate 
        // package with all the DB APIs wrapped around and published to the users.
        // Not doing it here to save some time.
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("tweetsdb");
        DBCollection usersCollection = db.getCollection("userscollection");

        // Not checking if the user is a valid user or not here.  That's a 
        // separate code flow on itself.
        DBObject query = new BasicDBObject("user", user);

        // Here I am retrieving the list of users the current user is following
        DBObject userDocument = usersCollection.findOne(query);
        if (userDocument == null) {
            result.put(user, "No such user");
            result.put("isSuccess", false);
            sendResponse(response, result);
            return null;
        }
        JSONObject json = (JSONObject) new JSONParser().parse(userDocument.toString());
        JSONArray following = (JSONArray) json.get("following");
        // A sample of the following array looks like ["user4", "user3"]
        List<String> followingList = getFollowingList(following);

        // Once the following list is retrieved, the tweets of those users are
        // read from the db.
        JSONArray tweetsArray = retrieveTweetsFromDB(db, followingList, count);

        result.put("tweets", tweetsArray);
        result.put("isSuccess", true);
    } else {
        System.out.println("Missing user parameter in the request.  Returning error");
        result.put("Missing parameter", "user");
        result.put("isSuccess", false);
    }
    sendResponse(response, result);
    return null;
}

From source file:halive.shootinoutside.common.util.Vector2D.java

public JSONObject toJSONObject() {
    JSONObject obj = new JSONObject();
    obj.put("x", Float.floatToIntBits(x));
    obj.put("y", Float.floatToIntBits(y));
    return obj;//from  w w w.  j  ava2  s .c o  m
}

From source file:capabilities.Browser.java

@Override
public String adaptRole(String dbResult) throws Exception {
    // Variveis retornadas do WURFL em JSON
    String mobile_browser;/*from  w w  w . j a  va 2  s .c  o  m*/
    String mobile_browser_version;

    // Conversao do JSON de entrada para as variaveis respectivas
    JSONObject capabilities;
    JSONParser parser = new JSONParser();
    capabilities = (JSONObject) parser.parse(dbResult);
    //JSONObject capabilities = (JSONObject) my_obj.get("capabilities");
    System.out.println("\t" + capabilities);

    mobile_browser = (String) capabilities.get("mobile_browser");
    mobile_browser_version = (String) capabilities.get("mobile_browser_version");

    // Olhar as APIs compatveis em http://mobilehtml5.org/
    // para possveis regras.

    // Criar um novo JSON e adicionar as informaes  ele.
    JSONObject virtual = new JSONObject();

    if (mobile_browser.equals("Firefox Mobile")) {
        virtual.put("luminosity", "true");
        virtual.put("proximity", "true");
        virtual.put("vibration", "true");
    } else {
        virtual.put("luminosity", "false");
        virtual.put("proximity", "false");
        virtual.put("vibration", "false");
    }

    if (mobile_browser.equals("Opera Mini")) {
        virtual.put("geolocation", "false");
    } else {
        virtual.put("geolocation", "true");
    }

    if (mobile_browser.equals("IEMobile")) {
        virtual.put("cameraAPI", "false");
        virtual.put("orientation", "false");
    } else {
        virtual.put("cameraAPI", "true");
        virtual.put("orientation", "true");
    }

    if (mobile_browser.equals("Chrome Mobile")) {
        virtual.put("webSpeechAPI", "true");
    } else {
        virtual.put("webSpeechAPI", "false");
    }

    // Adicionar esse novo JSON ao JSON de entrada e retorn-lo
    capabilities.put("virtual", virtual);
    return capabilities.toJSONString();
}

From source file:me.timothy.ddd.quests.BasicQuest.java

@SuppressWarnings("unchecked")
@Override//from w  w w .ja  v  a  2s  .c o m
public JSONObject asObject() {
    JSONObject result = new JSONObject();
    result.put("class", getClass().getName());
    return result;
}

From source file:net.sf.okapi.filters.drupal.Node.java

@SuppressWarnings("unchecked")
public Node(String language, String type, String title, String body) {
    store = new JSONObject();
    store.put("language", language);
    store.put("type", type);
    store.put("title", title);
    setBody(language, body);/*from   w  ww  .  j  a v  a  2 s.c  o m*/
}

From source file:cloudworker.Animoto.java

@SuppressWarnings("unchecked")
public void run() {
    //Get queue url   
    GetQueueUrlResult urlResult = sqs.getQueueUrl(responseQName);
    String QueueUrl = urlResult.getQueueUrl();
    JSONObject result = new JSONObject();

    Runtime runtime = Runtime.getRuntime();

    try {/*  ww  w  .j  a va 2 s  . c o  m*/
        String[] urls = task.split(" ");

        for (String url : urls) {
            //System.out.println(url);
            Process p = runtime.exec("wget " + url);
            p.waitFor();
        }

        Process rename = runtime.exec("./rename.sh");
        rename.waitFor();

        runtime.exec("ffmpeg -f image2 -i img%03d.jpg movie.mpg");

        File movie = new File("movie.mpg");

        S3Service s3 = new S3Service();
        URL url = s3.put(movie.getName(), movie);

        //result.put("task_id", task_id);
        result.put("URL", url.toString());

        sqs.sendMessage(new SendMessageRequest(QueueUrl, result.toString()));
        //System.out.println(Thread.currentThread().getName()+" sleep done!");

    } catch (Exception e) {
        //result.put("task_id", task_id);
        result.put("URL", "Failed!");
        sqs.sendMessage(new SendMessageRequest(QueueUrl, result.toString()));

    }
}

From source file:it.polimi.geinterface.DAO.Entity.java

private Entity(Type t) {
    properties = new JSONObject();
    properties.put(JsonStrings.PROPERTIES, new JSONObject());
    this.entityType = t;
}

From source file:de.Keyle.MyPet.util.configuration.ConfigurationJSON.java

public boolean load() {
    config = new JSONObject();
    BufferedReader reader = null;
    try {//w w  w.  jav  a2 s. c o  m
        reader = new BufferedReader(new FileReader(jsonFile));
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(reader);
        config = (JSONObject) obj;
    } catch (ParseException e) {
        MyPetLogger.write(ChatColor.RED + "Could not parse/load " + jsonFile.getName());
        DebugLogger.warning("Could not parse/load " + jsonFile.getName());
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        DebugLogger.printThrowable(e);
        return false;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
    return true;
}

From source file:com.megacasting_ppe.web.Servletloadpageflux.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w ww . j a v a 2 s.  c  o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json;  charset=ISO-8859-1");
    HttpSession session = request.getSession();

    JSONObject global = new JSONObject();

    Diffuseur diffuseur = (Diffuseur) session.getAttribute("DiffuseurObject");
    String connecterOk = (String) session.getAttribute("ConnecterDiffuseur");

    if (diffuseur != null && connecterOk != null) {

        JSONObject infoAuth = new JSONObject();

        infoAuth.put("Entreprise", diffuseur.getNomEntreprise());
        infoAuth.put("connecter", connecterOk);

        global.put("infoauth", infoAuth);

    } else {

        JSONObject infoAuth = new JSONObject();
        infoAuth.put("connecter", "false");

        global.put("infoauth", infoAuth);

    }
    ;

    try (PrintWriter out = response.getWriter()) {
        out.println(global.toString());
    }

}