本文整理汇总了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));
}