本文整理了Java中org.json.JSONObject.wrap()
方法的一些代码示例,展示了JSONObject.wrap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.wrap()
方法的具体详情如下:
包路径:org.json.JSONObject
类名称:JSONObject
方法名:wrap
[英]Wraps the given object if necessary.
If the object is null or , returns #NULL. If the object is a JSONArray or JSONObject, no wrapping is necessary. If the object is NULL, no wrapping is necessary. If the object is an array or Collection, returns an equivalent JSONArray. If the object is a Map, returns an equivalent JSONObject. If the object is a primitive wrapper type or String, returns the object. Otherwise if the object is from a java package, returns the result of toString. If wrapping fails, returns null.
[中]如有必要,包装给定对象。
如果对象为null或,则返回#null。如果对象是JSONArray或JSONObject,则不需要包装。如果对象为空,则不需要换行。如果对象是数组或集合,则返回等效的JSONArray。如果对象是映射,则返回等效的JSONObject。如果对象是基本包装类型或字符串,则返回该对象。否则,如果对象来自java包,则返回toString的结果。如果包装失败,则返回null。
代码示例来源:origin: commonsguy/cw-omnibus
private Object wrap(Object thingy) {
if (thingy instanceof CharSequence) {
return (JSONObject.wrap(thingy.toString()));
}
return (JSONObject.wrap(thingy));
}
}
代码示例来源:origin: apache/geode
/**
* Creates a new {@code JSONArray} by copying all values from the given collection.
*
* @param copyFrom a collection whose values are of supported types. Unsupported values are not
* permitted and will yield an array in an inconsistent state.
*/
/* Accept a raw type for API compatibility */
public JSONArray(Collection<?> copyFrom) {
this();
if (copyFrom != null) {
for (Object aCopyFrom : copyFrom) {
put(JSONObject.wrap(aCopyFrom));
}
}
}
代码示例来源:origin: zzz40500/GsonFormat
/**
* Construct a JSONArray from a Collection.
*
* @param collection
* A Collection.
*/
public JSONArray(Collection<Object> collection) {
this.myArrayList = new ArrayList<Object>();
if (collection != null) {
Iterator<Object> iter = collection.iterator();
while (iter.hasNext()) {
this.myArrayList.add(JSONObject.wrap(iter.next()));
}
}
}
代码示例来源:origin: commonsguy/cw-omnibus
private Object wrap(Object thingy) {
if (thingy instanceof Array) {
Object[] array=(Object[])thingy;
JSONArray jsonArray=new JSONArray();
for (Object o : array) {
jsonArray.put(wrap(o));
}
return(jsonArray);
}
if (thingy instanceof CharSequence) {
return(JSONObject.wrap(thingy.toString()));
}
return(JSONObject.wrap(thingy));
}
代码示例来源:origin: robovm/robovm
/**
* Creates a new {@code JSONArray} by copying all values from the given
* collection.
*
* @param copyFrom a collection whose values are of supported types.
* Unsupported values are not permitted and will yield an array in an
* inconsistent state.
*/
/* Accept a raw type for API compatibility */
public JSONArray(Collection copyFrom) {
this();
if (copyFrom != null) {
for (Iterator it = copyFrom.iterator(); it.hasNext();) {
put(JSONObject.wrap(it.next()));
}
}
}
代码示例来源:origin: loklak/loklak_server
/**
* Construct a JSONArray from a Collection.
*
* @param collection
* A Collection.
*/
public JSONArray(Collection<?> collection) {
if (collection == null) {
this.myArrayList = new ArrayList<Object>();
} else {
this.myArrayList = new ArrayList<Object>(collection.size());
for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
}
}
代码示例来源:origin: zzz40500/GsonFormat
/**
* Construct a JSONObject from a Map.
*
* @param map
* A map object that can be used to initialize the contents of
* the JSONObject.
* @throws JSONException
*/
public JSONObject(Map<String, Object> map) {
this.map = new HashMap<String, Object>();
if (map != null) {
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
Object value = entry.getValue();
if (value != null) {
this.map.put(entry.getKey(), wrap(value));
}
}
}
}
代码示例来源:origin: robovm/robovm
/**
* Creates a new {@code JSONArray} with values from the given primitive array.
*/
public JSONArray(Object array) throws JSONException {
if (!array.getClass().isArray()) {
throw new JSONException("Not a primitive array: " + array.getClass());
}
final int length = Array.getLength(array);
values = new ArrayList<Object>(length);
for (int i = 0; i < length; ++i) {
put(JSONObject.wrap(Array.get(array, i)));
}
}
代码示例来源:origin: zzz40500/GsonFormat
/**
* Construct a JSONArray from an array
*
* @throws JSONException
* If not an array.
*/
public JSONArray(Object array) throws JSONException {
this();
if (array.getClass().isArray()) {
int length = Array.getLength(array);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} else {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
}
代码示例来源:origin: robovm/robovm
/**
* Creates a new {@code JSONObject} by copying all name/value mappings from
* the given map.
*
* @param copyFrom a map whose keys are of type {@link String} and whose
* values are of supported types.
* @throws NullPointerException if any of the map's keys are null.
*/
/* (accept a raw type for API compatibility) */
public JSONObject(Map copyFrom) {
this();
Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom;
for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) {
/*
* Deviate from the original by checking that keys are non-null and
* of the proper type. (We still defer validating the values).
*/
String key = (String) entry.getKey();
if (key == null) {
throw new NullPointerException("key == null");
}
nameValuePairs.put(key, wrap(entry.getValue()));
}
}
代码示例来源:origin: apache/geode
/**
* Creates a new {@code JSONObject} by copying all name/value mappings from the given map.
*
* @param copyFrom a map whose keys are of type {@link String} and whose values are of supported
* types.
* @throws NullPointerException if any of the map's keys are null.
*/
/* (accept a raw type for API compatibility) */
public JSONObject(Map copyFrom) {
this();
Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom;
for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) {
/*
* Deviate from the original by checking that keys are non-null and of the proper type. (We
* still defer validating the values).
*/
String key = (String) entry.getKey();
if (key == null) {
throw new NullPointerException("key == null");
}
nameValuePairs.put(key, wrap(entry.getValue()));
}
}
代码示例来源:origin: apache/geode
/**
* Creates a new {@code JSONArray} with values from the given primitive array.
*
* @param array The values to use.
* @throws JSONException if any of the values are non-finite double values (i.e. NaN or infinite)
*/
public JSONArray(Object array) throws JSONException {
if (!array.getClass().isArray()) {
throw new JSONException("Not a primitive array: " + array.getClass());
}
final int length = Array.getLength(array);
values = new ArrayList<Object>(length);
for (int i = 0; i < length; ++i) {
put(JSONObject.wrap(Array.get(array, i)));
}
}
代码示例来源:origin: loklak/loklak_server
/**
* Construct a JSONObject from a Map.
*
* @param m
* A map object that can be used to initialize the contents of
* the JSONObject.
*/
public JSONObject(Map<?, ?> m) {
if (m == null) {
this.map = new HashMap<String, Object>();
} else {
this.map = new HashMap<String, Object>(m.size());
for (final Entry<?, ?> e : m.entrySet()) {
final Object value = e.getValue();
if (value != null) {
this.map.put(String.valueOf(e.getKey()), wrap(value));
}
}
}
}
代码示例来源:origin: loklak/loklak_server
/**
* Construct a JSONArray from an array
*
* @throws JSONException
* If not an array.
*/
public JSONArray(Object array) throws JSONException {
this();
if (array.getClass().isArray()) {
int length = Array.getLength(array);
this.myArrayList.ensureCapacity(length);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} else {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
}
代码示例来源:origin: zzz40500/GsonFormat
this.map.put(key, wrap(result));
代码示例来源:origin: apache/geode
props.put(key, wrap(result));
} else if (!method.getReturnType().isArray()) {
props.put(key, JSONObject.NULL);
代码示例来源:origin: loklak/loklak_server
final Object result = method.invoke(bean);
if (result != null) {
this.map.put(key, wrap(result));
代码示例来源:origin: b3log/latke
/**
* Construct a JSONArray from a Collection.
*
* @param collection
* A Collection.
*/
public JSONArray(Collection<?> collection) {
if (collection == null) {
this.myArrayList = new ArrayList<Object>();
} else {
this.myArrayList = new ArrayList<Object>(collection.size());
for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
}
}
代码示例来源:origin: b3log/latke
/**
* Construct a JSONObject from a Map.
*
* @param m
* A map object that can be used to initialize the contents of
* the JSONObject.
* @throws JSONException
* If a value in the map is non-finite number.
* @throws NullPointerException
* If a key in the map is <code>null</code>
*/
public JSONObject(Map<?, ?> m) {
if (m == null) {
this.map = new HashMap<String, Object>();
} else {
this.map = new HashMap<String, Object>(m.size());
for (final Entry<?, ?> e : m.entrySet()) {
if(e.getKey() == null) {
throw new NullPointerException("Null key.");
}
final Object value = e.getValue();
if (value != null) {
this.map.put(String.valueOf(e.getKey()), wrap(value));
}
}
}
}
代码示例来源:origin: b3log/latke
/**
* Construct a JSONArray from an array.
*
* @param array
* Array. If the parameter passed is null, or not an array, an
* exception will be thrown.
*
* @throws JSONException
* If not an array or if an array value is non-finite number.
* @throws NullPointerException
* Thrown if the array parameter is null.
*/
public JSONArray(Object array) throws JSONException {
this();
if (array.getClass().isArray()) {
int length = Array.getLength(array);
this.myArrayList.ensureCapacity(length);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} else {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
}
内容来源于网络,如有侵权,请联系作者删除!