本文整理匯總了Java中com.addthis.maljson.JSONObject類的典型用法代碼示例。如果您正苦於以下問題:Java JSONObject類的具體用法?Java JSONObject怎麽用?Java JSONObject使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JSONObject類屬於com.addthis.maljson包,在下文中一共展示了JSONObject類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testJsonToString
import com.addthis.maljson.JSONObject; //導入依賴的package包/類
@Test
public void testJsonToString() throws Exception {
List<String> list = Lists.newArrayList("zero", "one", "two");
ValueArray array = ValueFactory.createValueArray(list);
assertEquals(list.toString(), array.toString());
Bundle bundle = new ListBundle();
bundle.setValue(bundle.getFormat().getField("arrayField"), array);
Map<String, Object> bundleMap = Maps.newHashMap();
bundleMap.put("arrayField", list);
assertEquals(JSONObject.wrap(bundleMap).toString(), Bundles.toJsonString(bundle));
ValueMap valueMap = ValueFactory.createMap();
valueMap.put("arrayKey", array);
bundle.setValue(bundle.getFormat().getField("mapField"), valueMap);
Map<String, Object> nestedMap = Maps.newHashMap();
nestedMap.put("arrayKey", list);
bundleMap.put("mapField", nestedMap);
assertEquals(JSONObject.wrap(bundleMap).toString(), Bundles.toJsonString(bundle));
}
示例2: toJSONObject
import com.addthis.maljson.JSONObject; //導入依賴的package包/類
/**
* Create a new {@link JSONObject} as if calling the Bundle were a map passed to
* {@link JSONObject#JSONObject(Map)}. Fields containing {@link ValueArray} and
* {@link ValueMap} are recursively turned into {@link JSONArray}s and {@link JSONObject}s.
*
* The "JSONObject" capitalization scheme is kept to match the class name of the returned
* object.
*/
public static JSONObject toJSONObject(Bundle row) {
JSONObject jsonRow = new JSONObject();
for (BundleField field : row) {
ValueObject valueObject = row.getValue(field);
try {
jsonRow.put(field.getName(), jsonWrapValue(valueObject));
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
return jsonRow;
}
示例3: jsonWrapValue
import com.addthis.maljson.JSONObject; //導入依賴的package包/類
private static Object jsonWrapValue(ValueObject valueObject) {
if (valueObject == null) {
return null;
} else {
return JSONObject.wrap(valueObject.asNative());
}
}
示例4: encodeJSON
import com.addthis.maljson.JSONObject; //導入依賴的package包/類
/** @deprecated Use {@link #encodeJsonNode(Object)} or {@link CodecJackson} */
@Deprecated
public static JSONObject encodeJSON(Object object) throws Exception {
return new JSONObject(Jackson.defaultCodec().getObjectMapper().writeValueAsString(object));
}