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

The following examples show how to use org.json.JSONStringer#value() . 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: Utils.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * serialize String values in JSON format
 * 
 * @param array
 * @return
 */
public static String getJSON(String[] array) throws Exception
{
	try
	{
		JSONStringer js = new JSONStringer();
		js.array();
		if (array != null)
		{
			for (String value : array)
			{
				js.value(value);
			}
		}
		js.endArray();
		return js.toString();
	}
	catch (JSONException e)
	{
		throw new Exception("cannot produce JSON", e);
	}
}
 
Example 4
Source File: Evaluation.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public String toJSONString() {
  try {
    JSONStringer stringer =
        new JSONStringer().object().key(STATUS_KEY).value(status).key(VALUE_KEY);
    if ((value instanceof String)
        || (value instanceof Number)
        || (value instanceof Boolean)
        || (value == null)) {
      stringer.value(value);
    } else {
      String jsonValue = ModelCodec.encode(value);
      stringer.value(new JSONTokener(jsonValue).nextValue());
    }
    stringer.endObject();
    return stringer.toString();
  } catch (JSONException je) {
    throw new RuntimeException(je);
  }
}
 
Example 5
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 6
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testValueJSONNull() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(JSONObject.NULL);
    stringer.endArray();
    assertEquals("[null]", stringer.toString());
}
 
Example 7
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testArray() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(false);
    stringer.value(5.0);
    stringer.value(5L);
    stringer.value("five");
    stringer.value(null);
    stringer.endArray();
    assertEquals("[false,5,5,\"five\",null]", stringer.toString());
}
 
Example 8
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testValueObjectMethods() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(Boolean.FALSE);
    stringer.value(Double.valueOf(5.0));
    stringer.value(Long.valueOf(5L));
    stringer.endArray();
    assertEquals("[false,5,5]", stringer.toString());
}
 
Example 9
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test what happens when extreme values are emitted. Such values are likely
 * to be rounded during parsing.
 */
public void testNumericRepresentations() throws JSONException {
    if (System.getProperty("os.arch").equals("armv7")) {
      // On armv7, MIN_VALUE is indistinguishable from zero.
      return;
    }
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(Long.MAX_VALUE);
    stringer.value(Double.MIN_VALUE);
    stringer.endArray();
    assertEquals("[9223372036854775807,4.9E-324]", stringer.toString());
}
 
Example 10
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testJSONArrayAsValue() throws JSONException {
    JSONArray array = new JSONArray();
    array.put(false);
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(array);
    stringer.endArray();
    assertEquals("[[false]]", stringer.toString());
}
 
Example 11
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMaxDepthWithArrayValue() throws JSONException {
    JSONArray array = new JSONArray();
    array.put(false);

    JSONStringer stringer = new JSONStringer();
    for (int i = 0; i < 20; i++) {
        stringer.array();
    }
    stringer.value(array);
    for (int i = 0; i < 20; i++) {
        stringer.endArray();
    }
    assertEquals("[[[[[[[[[[[[[[[[[[[[[false]]]]]]]]]]]]]]]]]]]]]", stringer.toString());
}
 
Example 12
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 13
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 14
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 15
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();
}