Java Code Examples for org.json.JSONStringer#key()

The following examples show how to use org.json.JSONStringer#key() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreUtils.java    From Hook with Apache License 2.0 6 votes vote down vote up
public static void putMap(String key, Map<Integer, String> age2nameMap) {
    if (age2nameMap != null) {
        JSONStringer jsonStringer = new JSONStringer();
        try {
            jsonStringer.array();
            for (Integer integer : age2nameMap.keySet()) {
                jsonStringer.object();
                jsonStringer.key("age");
                jsonStringer.value(integer);
                jsonStringer.key("name");
                jsonStringer.value(age2nameMap.get(integer));
                jsonStringer.endObject();
            }
            jsonStringer.endArray();
        } catch (JSONException e) {
            e.printStackTrace();
        }
       sp.edit().putString(key, jsonStringer.toString()).commit();
    }
}
 
Example 2
Source File: Utils.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * serialize String keys and String values in JSON format, sort keys
 * according to order in sortedList
 * 
 * @param map
 * @return
 */
public static String getJSON(List<String> sortedList, Map<String, Object> map) throws Exception
{
	try
	{
		JSONStringer js = new JSONStringer();
		js.object();
		if (map != null)
		{
			for (String key : sortedList)
			{
				js.key(key);
				js.value(map.get(key));
			}
		}
		js.endObject();
		return js.toString();
	}
	catch (JSONException e)
	{
		throw new Exception("cannot produce JSON", e);
	}
}
 
Example 3
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testMaxDepthWithObjectValue() throws JSONException {
    JSONObject object = new JSONObject();
    object.put("a", false);
    JSONStringer stringer = new JSONStringer();
    for (int i = 0; i < 20; i++) {
        stringer.object();
        stringer.key("b");
    }
    stringer.value(object);
    for (int i = 0; i < 20; i++) {
        stringer.endObject();
    }
    assertEquals("{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":" +
            "{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":" +
            "{\"a\":false}}}}}}}}}}}}}}}}}}}}}", stringer.toString());
}
 
Example 4
Source File: JsonEncoder.java    From egads with GNU General Public License v3.0 5 votes vote down vote up
public static void // modifies json_out
toJson(Object object, JSONStringer json_out) throws Exception {
    json_out.object();
    // for each inherited class...
    for (Class c = object.getClass(); c != Object.class; c = c
            .getSuperclass()) {
        // for each member variable... 
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            // if variable is static/private... skip it
            if (Modifier.isStatic(f.getModifiers())) {
                continue;
            }
            if (Modifier.isPrivate(f.getModifiers())) {
                continue;
            }
            Object value = f.get(object);

            // if variable is a complex type... recurse on sub-objects
            if (value instanceof JsonAble) {
                json_out.key(f.getName());
                ((JsonAble) value).toJson(json_out);
                // if variable is an array... recurse on sub-objects
            } else if (value instanceof ArrayList) {
                json_out.key(f.getName());
                json_out.array();
                for (Object e : (ArrayList) value) {
                    toJson(e, json_out);
                }
                json_out.endArray();
                // if variable is a simple type... convert to json
            } else {
                json_out.key(f.getName()).value(value);
            }
        }
    }
    json_out.endObject();
}
 
Example 5
Source File: Settings.java    From onedev with MIT License 4 votes vote down vote up
public CharSequence toJson() {
	try {
		JSONStringer writer = new JSONStringer();
		writer.object();
		Json.writeObject(writer, "minimumInputLength", minimumInputLength);
		Json.writeObject(writer, "minimumResultsForSearch", minimumResultsForSearch);
		Json.writeObject(writer, "maximumSelectionSize", maximumSelectionSize);
		Json.writeObject(writer, "placeholder", placeholder);
		Json.writeObject(writer, "allowClear", allowClear);
		Json.writeObject(writer, "multiple", multiple);
		Json.writeObject(writer, "closeOnSelect", closeOnSelect);
		Json.writeFunction(writer, "id", id);
		Json.writeFunction(writer, "matcher", matcher);
		Json.writeFunction(writer, "tokenizer", tokenizer);
		Json.writeFunction(writer, "sortResults", sortResults);
		Json.writeFunction(writer, "formatSelection", formatSelection);
		Json.writeFunction(writer, "formatResult", formatResult);
		Json.writeFunction(writer, "formatNoMatches", formatNoMatches);
		Json.writeFunction(writer, "formatInputTooShort", formatInputTooShort);
		Json.writeFunction(writer, "formatResultCssClass", formatResultCssClass);
		Json.writeFunction(writer, "formatSelectionTooBig", formatSelectionTooBig);
		Json.writeFunction(writer, "formatLoadMore", formatLoadMore);
		Json.writeFunction(writer, "formatSearching", formatSearching);
		Json.writeFunction(writer, "escapeMarkup", escapeMarkup);
		Json.writeFunction(writer, "createSearchChoice", createSearchChoice);
		Json.writeFunction(writer, "initSelection", initSelection);
		Json.writeFunction(writer, "query", query);
		Json.writeObject(writer, "width", width);
		Json.writeObject(writer, "openOnEnter", openOnEnter);
		Json.writeFunction(writer, "containerCss", containerCss);
		Json.writeObject(writer, "containerCssClass", containerCssClass);
		Json.writeFunction(writer, "dropdownCss", dropdownCss);
		Json.writeObject(writer, "dropdownCssClass", dropdownCssClass);
		Json.writeObject(writer, "separator", separator);
		Json.writeObject(writer, "tokenSeparators", tokenSeparators);
		Json.writeObject(writer, "dropdownAutoWidth", dropdownAutoWidth);
		if (ajax != null) {
			writer.key("ajax");
			ajax.toJson(writer);
		}
		Json.writeFunction(writer, "data", data);
		Json.writeFunction(writer, "tags", tags);
		writer.endObject();

		return writer.toString();
	} catch (JSONException e) {
		throw new RuntimeException("Could not convert Select2 settings object to Json", e);
	}
}
 
Example 6
Source File: SqlDBOperate.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
public String sendUserInfoToJSON() {
    List<UserInfo> users;
    int count = (int) getCountOfUserInfo();
    users = getScrollDataOfUserInfo(0, count);
    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("user");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (UserInfo user : users) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(user.getId());
            jsonText.key("name");
            jsonText.value(user.getName());
            jsonText.key("sex");
            jsonText.value(user.getSex());
            jsonText.key("age");
            jsonText.value(user.getAge());
            jsonText.key("IMEI");
            jsonText.value(user.getIMEI());
            jsonText.key("ip");
            jsonText.value(user.getIPAddr());
            jsonText.key("status");
            jsonText.value(user.getIsOnline());
            jsonText.key("avater");
            jsonText.value(user.getAvater());
            jsonText.key("lastdate");
            jsonText.value(user.getLastDate());
            jsonText.key("device");
            jsonText.value(user.getDevice());
            jsonText.key("constellation");
            jsonText.value(user.getConstellation());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}
 
Example 7
Source File: SqlDBOperate.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
public String sendChattingInfoToJSON(int sendID, int receiverID) {
    List<ChattingInfo> infos;

    infos = getAllMessageFromChattingInfo(sendID, receiverID);

    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("chatting");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (ChattingInfo info : infos) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(info.getId());
            jsonText.key("sendID");
            jsonText.value(info.getSendID());
            jsonText.key("receiverID");
            jsonText.value(info.getReceiverID());
            jsonText.key("chatting");
            jsonText.value(info.getInfo());
            jsonText.key("date");
            jsonText.value(info.getDate());
            jsonText.key("style");
            jsonText.value(info.getStyle());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}
 
Example 8
Source File: SqlDBOperate.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
public String sendUserInfoToJSON() {
    List<UserInfo> users;
    int count = (int) getCountOfUserInfo();
    users = getScrollDataOfUserInfo(0, count);
    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("user");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (UserInfo user : users) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(user.getId());
            jsonText.key("name");
            jsonText.value(user.getName());
            jsonText.key("sex");
            jsonText.value(user.getSex());
            jsonText.key("age");
            jsonText.value(user.getAge());
            jsonText.key("IMEI");
            jsonText.value(user.getIMEI());
            jsonText.key("ip");
            jsonText.value(user.getIPAddr());
            jsonText.key("status");
            jsonText.value(user.getIsOnline());
            jsonText.key("avater");
            jsonText.value(user.getAvater());
            jsonText.key("lastdate");
            jsonText.value(user.getLastDate());
            jsonText.key("device");
            jsonText.value(user.getDevice());
            jsonText.key("constellation");
            jsonText.value(user.getConstellation());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}
 
Example 9
Source File: SqlDBOperate.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
public String sendChattingInfoToJSON(int sendID, int receiverID) {
    List<ChattingInfo> infos;

    infos = getAllMessageFromChattingInfo(sendID, receiverID);

    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("chatting");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (ChattingInfo info : infos) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(info.getId());
            jsonText.key("sendID");
            jsonText.value(info.getSendID());
            jsonText.key("receiverID");
            jsonText.value(info.getReceiverID());
            jsonText.key("chatting");
            jsonText.value(info.getInfo());
            jsonText.key("date");
            jsonText.value(info.getDate());
            jsonText.key("style");
            jsonText.value(info.getStyle());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}