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:org.c96.db.DAO.java

/**
 * ()//ww  w  .  j a  v  a 2  s  . c o  m
 *
 * @return ?JSONARRAY DATA
 * @throws SQLException
 */
static public JSONArray getJsonArrayData() throws SQLException {
    JSONArray jData = null;

    try {
        //?
        con = DBConnection.getConnection();

        if (sql.equals("")) {
            throw new Exception("SQL ");
        }

        ps = con.prepareStatement(sql);
        //?
        for (int i = 0; i < jParameters.length(); i++) {
            ps.setObject(i + 1, jParameters.get(i));
        }
        rs = ps.executeQuery();

        jData = new JSONArray();
        JSONObject jtmp = null;
        while (rs.next()) {
            jtmp = new JSONObject();
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                jtmp.put(rs.getMetaData().getColumnLabel(i), rs.getObject(i));
            }
            jData.put(jtmp);
        }
        Status = "Success";
    } catch (Exception ex) {
        System.out.println("SQL Error : " + ex.getMessage());
    } finally {
        if (con != null) {
            con.close();
        }
    }

    return jData;
}

From source file:org.charvolant.argushiigi.server.ReferenceQueryResource.java

/**
 * Get a list of matching resources as a JSON document.
 *
 * @see org.restlet.resource.ServerResource#get()
 *///from  ww w  .  j  a  v a 2  s. c o m
@Get
public Representation get() {
    String resource = Reference.decode(this.getQueryValue(this.PARAM_RESOURCE));

    try {
        JSONObject wrap = new JSONObject();
        JSONArray result = new JSONArray();
        StmtIterator si = this.model.listStatements(null, null, this.model.createResource(resource));
        Comparator<Resource> classComparator = this.sorter.getClassComparator();

        while (si.hasNext()) {
            Statement s = si.next();
            Resource res = s.getSubject();

            if (res.isURIResource()) {
                JSONObject obj = new JSONObject();
                Reference ref = this.application.getLocalRef(new Reference(res.getURI()));
                StmtIterator ci = this.model.listStatements(res, RDF.type, (Resource) null);
                Resource cls = null;

                obj.put("name", this.sorter.getName(s.getSubject(), Locale.ENGLISH));
                obj.put("uri", s.getSubject().getURI());
                obj.put("href", ref);
                while (ci.hasNext()) {
                    Resource c = ci.next().getResource();

                    if (c.isAnon())
                        continue;
                    if (cls == null || classComparator.compare(cls, c) > 0)
                        cls = c;
                }
                if (cls == null)
                    cls = OWL.Thing;
                ref = this.application.getLocalRef(new Reference(cls.getURI()));
                obj.put("cls", this.sorter.getName(cls, Locale.ENGLISH));
                obj.put("clsUri", cls.getURI());
                obj.put("clsHref", ref);
                result.put(obj);
            }
        }
        wrap.put("aaData", result);
        return new JsonRepresentation(wrap);
    } catch (Exception ex) {
        this.application.getLogger().log(Level.SEVERE, "Unable to get references to " + resource, ex);
        this.getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, ex);
        return new StringRepresentation(ex.getMessage());
    }
}

From source file:cn.geowind.takeout.verify.JsonRestApi.java

/**
 * @brief ??//from  w  w w.j a  v a 2  s.com
 * @param accountSid
 *            ?
 * @param authToken
 *            ?
 * @param appId
 *            id
 * @param to
 *            ?
 * @return HttpPost ???
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public String sendTemplateSMS(String accountSid, String authToken, String appId, String to, String code)
        throws NoSuchAlgorithmException, KeyManagementException {
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("app.cloopen.com", "TLS", 8883, "https");

    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id + ? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);

        StringBuffer sb = new StringBuffer();
        String url = sb.append(SMS_BASE_URL).append(SMS_VERSION).append("/Accounts/").append(accountSid)
                .append(SMS).append(TEMPLATE_SMS).append("?sig=").append(signature).toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);// base64(Id + ? +
        // )
        JSONObject obj = new JSONObject();
        obj.put("to", to);
        obj.put("appId", appId);
        obj.put("templateId", SMS_TEMPLATE_ID);
        JSONArray replace = new JSONArray();
        replace.put(code);
        obj.put("datas", replace);
        String data = obj.toString();

        System.out.println("---------------------------- SendSMS for JSON begin----------------------------");
        System.out.println("data: " + data);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(data.getBytes("UTF-8")));
        requestBody.setContentLength(data.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    // ???
    return result;
}

From source file:com.bangz.shotrecorder.SplitManager.java

String toJSONString() {
    JSONArray jsonArray = new JSONArray();

    int SHOTS = getNumbers();

    for (int i = 0; i < SHOTS; i++) {
        jsonArray.put(getSplits().get(i).getTime());
    }//from w w  w  . ja  v a 2  s .  c  o m

    return jsonArray.toString();

}

From source file:com.basho.riak.client.mapreduce.filter.FloatToStringFilter.java

public JSONArray toJson() {
    JSONArray filter = new JSONArray();
    filter.put("float_to_string");
    return filter;
}

From source file:com.jvoid.products.controller.JVoidProductController.java

@RequestMapping(value = "attributevalue/update", method = RequestMethod.GET)
public @ResponseBody String updateAttributeValue(
        @RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    String strJson = "{\"attributeValues\":[{\"id\":1,\"value\":\"6\",\"language\":\"enUS\",\"attribute_id\":1,\"position\":1},{\"id\":2,\"value\":\"7\",\"language\":\"enUS\",\"attribute_id\":1,\"position\":2},{\"id\":3,\"value\":\"8\",\"language\":\"enUS\",\"attribute_id\":1,\"position\":3}]}";
    JSONObject mainJObj = new JSONObject();
    JSONArray jArray = new JSONArray();
    try {/*from   www . j av a2s. c  o m*/
        for (int i = 0; i < 3; i++) {
            JSONObject jObj = new JSONObject();
            jObj.put("attribute_id", 1);
            int id = i + 1;
            int value = 6 + i;
            String strValue = String.valueOf(value) + "test";
            jObj.put("id", (i + 1));
            jObj.put("value", strValue);
            jObj.put("position", (i + 1));
            jObj.put("language", "enUS");
            jArray.put(jObj);
        }
        mainJObj.put("attributeValues", jArray);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    System.out.println(mainJObj.toString());

    int status = this.productsMasterService.updateAttributeValue(mainJObj);

    JSONObject jsonObject = new JSONObject();
    if (status == 1) {
        try {
            jsonObject.put("status", 1);
            jsonObject.put("message", "AttributeValues updated successfully.");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        try {
            jsonObject.put("status", 0);
            jsonObject.put("message", "Failed to update AttributeValues.");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return jsonObject.toString();

}

From source file:com.jvoid.products.controller.JVoidProductController.java

@RequestMapping(value = "attributevalue/add", method = RequestMethod.GET)
public @ResponseBody String addAttributeValue(
        @RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    String strJson = "{\"attributeValues\":[{\"value\":\"6\",\"language\":\"enUS\",\"attribute_id\":1,\"position\":1},{\"value\":\"7\",\"language\":\"enUS\",\"attribute_id\":1,\"position\":2},{\"value\":\"8\",\"language\":\"enUS\",\"attribute_id\":1,\"position\":3}]}";
    JSONObject mainJObj = new JSONObject();
    JSONArray jArray = new JSONArray();
    try {//from w w  w. j  a v a  2 s . c  o m
        for (int i = 0; i < 3; i++) {
            JSONObject jObj = new JSONObject();
            jObj.put("attribute_id", 1);
            int value = 6 + i;
            String strValue = String.valueOf(value);
            jObj.put("value", strValue);
            jObj.put("position", (i + 1));
            jObj.put("language", "enUS");
            jArray.put(jObj);
        }
        mainJObj.put("attributeValues", jArray);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    System.out.println(mainJObj.toString());

    int status = this.productsMasterService.addAttributeValue(mainJObj);

    JSONObject jsonObject = new JSONObject();
    if (status == 1) {
        try {
            jsonObject.put("status", 1);
            jsonObject.put("message", "AttributeValues inserted successfully.");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        try {
            jsonObject.put("status", 0);
            jsonObject.put("message", "Failed to insert AttributeValues.");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return jsonObject.toString();

}

From source file:com.jvoid.products.controller.JVoidProductController.java

@RequestMapping(value = "catalog/category", method = RequestMethod.GET)
public @ResponseBody String listCategory(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    JSONObject test = new JSONObject();
    try {//from  www  . ja va2s .  c om
        test.put("id", -1);
    } catch (JSONException e2) {
        e2.printStackTrace();
    }
    JSONObject jparams = jsonParams;
    int cid = -1;
    try {
        cid = jparams.getInt("id");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    List<CategoryMaster> categoryMasterList = null;
    if (cid > 0) {
        CategoryMaster categoryMaster = this.categoryProductsMasterService.getCategoryById(cid);
        List<CategoryMaster> cmList = new ArrayList<>();

        cmList.add(categoryMaster);
        categoryMasterList = cmList;
    } else if (cid == -1) {
        categoryMasterList = this.categoryProductsMasterService.getAllCategories();
    }

    JSONObject categoryListObject = new JSONObject();
    JSONArray categories = new JSONArray();
    ObjectMapper mapper = new ObjectMapper();
    for (int i = 0; i < categoryMasterList.size(); i++) {

        try {
            String strCategoryMasterObj = mapper.writeValueAsString(categoryMasterList.get(i));
            JSONObject jsonObj = new JSONObject(strCategoryMasterObj);

            categories.put(jsonObj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    try {
        categoryListObject.put("categories", categories);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    return categoryListObject.toString();
}

From source file:com.jvoid.products.controller.JVoidProductController.java

@RequestMapping(value = "catalog/product", method = RequestMethod.GET)
public @ResponseBody String getProductById(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    JSONObject test = new JSONObject();
    try {//from w w w  .  j a v a 2s. c om
        test.put("id", -1);
    } catch (JSONException e2) {
        e2.printStackTrace();
    }

    JSONObject jparams = jsonParams;

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String json = "";

    //System.out.println("TOTAL RECS1:"+this.productsMasterService.getAllProducts().size());

    int pid = -1;
    try {
        pid = jparams.getInt("id");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    List<ProductsMaster> productsList = null;
    if (pid > 0) {
        List<ProductsMaster> pmList = new ArrayList<>();
        ProductsMaster productsMaster = this.productsMasterService.getProductById(pid);
        pmList.add(productsMaster);
        productsList = pmList;
    } else {
        productsList = this.productsMasterService.getAllProducts();
    }

    JSONObject productListObject = new JSONObject();
    JSONArray products = new JSONArray();
    ObjectMapper mapper = new ObjectMapper();
    for (int i = 0; i < productsList.size(); i++) {

        try {
            String strProductMasterObj = mapper.writeValueAsString(productsList.get(i));
            JSONObject jsonObj = new JSONObject(strProductMasterObj);

            products.put(jsonObj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    try {
        productListObject.put("products", products);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    //productsList.add(arg0)
    json = productListObject.toString();
    return json;
    /*try {
       ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
       if (null == callback) {
     json = ow.writeValueAsString(productListObject);
        } else {
     json = callback + "(" + ow.writeValueAsString(productListObject) + ")";
        }
        System.out.println(json);
        out.write(json.getBytes());
       response.setContentType("application/json;charset=UTF-8");
       response.setHeader("Connection", "Keep-Alive");
       response.setHeader("Keep-Alive", "timeout=5, max=100");
        response.setContentLength(out.size());
        response.getOutputStream().write(out.toByteArray());
        response.getOutputStream().flush();
     } catch (IOException e) {
    e.printStackTrace();
     }*/

}

From source file:com.jvoid.products.controller.JVoidProductController.java

@RequestMapping(value = "catalog/categoryproduct", method = RequestMethod.GET)
public @ResponseBody String getProductByCategory(
        @RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    JSONObject test = new JSONObject();
    try {//from www .jav  a2s  .co m
        test.put("id", -1);
    } catch (JSONException e2) {
        e2.printStackTrace();
    }

    JSONObject jparams = jsonParams;

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String json = "";

    int cid = -1;
    try {
        cid = jparams.getInt("id");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    List<ProductsMaster> productsList = null;

    if (cid > 0) {
        productsList = this.productsMasterService.getProductByCategory(cid);
    } else {
        productsList = this.productsMasterService.getAllProducts();
    }

    JSONObject productListObject = new JSONObject();
    JSONArray products = new JSONArray();
    ObjectMapper mapper = new ObjectMapper();
    for (int i = 0; i < productsList.size(); i++) {

        try {
            String strProductMasterObj = mapper.writeValueAsString(productsList.get(i));
            JSONObject jsonObj = new JSONObject(strProductMasterObj);

            products.put(jsonObj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        productListObject.put("products", products);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    //productsList.add(arg0)
    json = productListObject.toString();
    return json;
}