Example usage for org.json JSONArray JSONArray

List of usage examples for org.json JSONArray JSONArray

Introduction

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

Prototype

public JSONArray() 

Source Link

Document

Construct an empty JSONArray.

Usage

From source file:org.rssin.serialization.JsonSerializer.java

public static JSONArray numbersListToJson(List<? extends Number> list) throws JSONException {
    JSONArray json = new JSONArray();
    for (Number item : list) {
        json.put(item);//from ww w.  java  2 s  . com
    }
    return json;
}

From source file:br.unicamp.cst.behavior.bn.Behavior.java

/**
 * Clears this behavior's action list/*w  w  w . j a v  a 2 s  .c om*/
 */
public void clearActionList() {
    this.jsonActionList = new JSONArray();
    this.actionList.clear();
}

From source file:org.loklak.api.iot.AbstractPushServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Query post = RemoteAccess.evaluate(request);
    String remoteHash = Integer.toHexString(Math.abs(post.getClientHost().hashCode()));

    // manage DoS
    if (post.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;/* ww w  .j a  va2s  .  c o m*/
    }

    String url = post.get("url", "");
    if (url == null || url.length() == 0) {
        response.sendError(400, "your request does not contain an url to your data object");
        return;
    }
    String screen_name = post.get("screen_name", "");
    if (screen_name == null || screen_name.length() == 0) {
        response.sendError(400, "your request does not contain required screen_name parameter");
        return;
    }

    JSONObject map;
    String jsonString = "";
    try {
        jsonString = new String(ClientConnection.download(url), StandardCharsets.UTF_8);
        map = new JSONObject(jsonString);
    } catch (Exception e) {
        response.sendError(400, "error reading json file from url");
        return;
    }

    // validation phase
    ProcessingReport report = this.validator.validate(jsonString);
    if (!report.isSuccess()) {
        response.sendError(400,
                "json does not conform to schema : " + this.getValidatorSchema().name() + "\n" + report);
        return;
    }

    // conversion phase
    Object extractResults = extractMessages(map);
    JSONArray messages;
    if (extractResults instanceof JSONArray) {
        messages = (JSONArray) extractResults;
    } else if (extractResults instanceof JSONObject) {
        messages = new JSONArray();
        messages.put((JSONObject) extractResults);
    } else {
        throw new IOException("extractMessages must return either a List or a Map. Get "
                + (extractResults == null ? "null" : extractResults.getClass().getCanonicalName())
                + " instead");
    }
    JSONArray convertedMessages = this.converter.convert(messages);

    PushReport nodePushReport = new PushReport();
    ObjectWriter ow = new ObjectMapper().writerWithDefaultPrettyPrinter();
    // custom treatment for each message
    for (int i = 0; i < messages.length(); i++) {
        JSONObject message = (JSONObject) convertedMessages.get(i);
        message.put("source_type", this.getSourceType().toString());
        message.put("location_source", LocationSource.USER.name());
        message.put("place_context", PlaceContext.ABOUT.name());
        if (message.get("text") == null) {
            message.put("text", "");
        }

        // append rich-text attachment
        String jsonToText = ow.writeValueAsString(messages.get(i));
        message.put("text", message.get("text") + MessageEntry.RICH_TEXT_SEPARATOR + jsonToText);
        customProcessing(message);

        if (message.get("mtime") == null) {
            String existed = PushServletHelper.checkMessageExistence(message);
            // message known
            if (existed != null) {
                messages.remove(i);
                nodePushReport.incrementKnownCount(existed);
                continue;
            }
            // updated message -> save with new mtime value
            message.put("mtime", Long.toString(System.currentTimeMillis()));
        }

        try {
            message.put("id_str", PushServletHelper.computeMessageId(message, getSourceType()));
        } catch (Exception e) {
            DAO.log("Problem computing id : " + e.getMessage());
        }
    }
    try {
        PushReport savingReport = PushServletHelper.saveMessagesAndImportProfile(convertedMessages,
                jsonString.hashCode(), post, getSourceType(), screen_name);
        nodePushReport.combine(savingReport);
    } catch (IOException e) {
        response.sendError(404, e.getMessage());
        return;
    }
    String res = PushServletHelper.buildJSONResponse(post.get("callback", ""), nodePushReport);

    post.setResponse(response, "application/javascript");
    response.getOutputStream().println(res);
    DAO.log(request.getServletPath() + " -> records = " + nodePushReport.getRecordCount() + ", new = "
            + nodePushReport.getNewCount() + ", known = " + nodePushReport.getKnownCount() + ", error = "
            + nodePushReport.getErrorCount() + ", from host hash " + remoteHash);
}

From source file:com.kakao.internal.Action.java

public JSONObject createJSONObject() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(KakaoTalkLinkProtocol.ACTION_TYPE, type.value);
    if (url != null) {
        json.put(KakaoTalkLinkProtocol.ACTION_URL, url);
    }/* w w w  .ja v  a 2  s.  c  o m*/
    if (appActionInfos != null) {
        JSONArray jsonObjs = new JSONArray();
        for (AppActionInfo obj : appActionInfos) {
            jsonObjs.put(obj.createJSONObject());
        }
        json.put(KakaoTalkLinkProtocol.ACTION_ACTIONINFO, jsonObjs);
    }
    return json;
}

From source file:org.uiautomation.ios.server.servlet.IOSServlet.java

private JSONArray serializeStackTrace(StackTraceElement[] els) throws JSONException {
    JSONArray stacktace = new JSONArray();
    for (StackTraceElement el : els) {
        JSONObject stckEl = new JSONObject();
        stckEl.put("fileName", el.getFileName());
        stckEl.put("className", el.getClassName());
        stckEl.put("methodName", el.getMethodName());
        stckEl.put("lineNumber", el.getLineNumber());
        stacktace.put(stckEl);//ww w  . j  av a  2  s  .  co  m
    }
    return stacktace;
}