Compartilhar via


JSONObject Class

Definition

A modifiable set of name/value mappings.

[Android.Runtime.Register("org/json/JSONObject", DoNotGenerateAcw=true)]
public class JSONObject : Java.Lang.Object
[<Android.Runtime.Register("org/json/JSONObject", DoNotGenerateAcw=true)>]
type JSONObject = class
    inherit Object
Inheritance
JSONObject
Attributes

Remarks

A modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of JSONObject JSONObjects, JSONArray JSONArrays, Strings, Booleans, Integers, Longs, Doubles or #NULL. Values may not be null, Double#isNaN() NaNs, Double#isInfinite() infinities, or of any type not listed here.

This class can coerce values to another type when requested. <ul> <li>When the requested type is a boolean, strings will be coerced using a case-insensitive comparison to "true" and "false". <li>When the requested type is a double, other Number types will be coerced using Number#doubleValue() doubleValue. Strings that can be coerced using Double#valueOf(String) will be. <li>When the requested type is an int, other Number types will be coerced using Number#intValue() intValue. Strings that can be coerced using Double#valueOf(String) will be, and then cast to int. <li>"lossy">When the requested type is a long, other <c>Number</c> types will be coerced using <c>Number#longValue() longValue</c>. Strings that can be coerced using <c>Double#valueOf(String)</c> will be, and then cast to long. This two-step conversion is lossy for very large values. For example, the string "9223372036854775806" yields the long 9223372036854775807.<li>When the requested type is a String, other non-null values will be coerced using String#valueOf(Object). Although null cannot be coerced, the sentinel value JSONObject#NULL is coerced to the string "null". </ul>

This class can look up both mandatory and optional values: <ul> <li>Use get<i>Type</i>() to retrieve a mandatory value. This fails with a JSONException if the requested name has no value or if the value cannot be coerced to the requested type. <li>Use opt<i>Type</i>() to retrieve an optional value. This returns a system- or user-supplied default if the requested name has no value or if the value cannot be coerced to the requested type. </ul>

<strong>Warning:</strong> this class represents null in two incompatible ways: the standard Java null reference, and the sentinel value JSONObject#NULL. In particular, calling put(name, null) removes the named entry from the object but put(name, JSONObject.NULL) stores an entry whose value is JSONObject.NULL.

Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java, 3rd edition Item 19, "Design and Document for inheritance or else prohibit it" for further information.

Java documentation for org.json.JSONObject.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

JSONObject()

Creates a JSONObject with no name/value mappings.

JSONObject(IDictionary)

Creates a new JSONObject by copying all name/value mappings from the given map.

JSONObject(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

JSONObject(JSONObject, String[])

Creates a new JSONObject by copying mappings for the listed names from the given object.

JSONObject(JSONTokener)

Creates a new JSONObject with name/value mappings from the next object in the tokener.

JSONObject(String)

Creates a new JSONObject with name/value mappings from the JSON string.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Null

A sentinel value used to explicitly define a name with no value.

PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Accumulate(String, Object)

Appends value to the array already mapped to name.

Append(String, Object)

Appends values to the array mapped to name.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Get(String)

Returns the value mapped by name, or throws if no such mapping exists.

GetBoolean(String)

Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean, or throws otherwise.

GetDouble(String)

Returns the value mapped by name if it exists and is a double or can be coerced to a double, or throws otherwise.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetInt(String)

Returns the value mapped by name if it exists and is an int or can be coerced to an int, or throws otherwise.

GetJSONArray(String)

Returns the value mapped by name if it exists and is a JSONArray, or throws otherwise.

GetJSONObject(String)

Returns the value mapped by name if it exists and is a JSONObject, or throws otherwise.

GetLong(String)

Returns the value mapped by name if it exists and is a long or can be coerced to a long, or throws otherwise.

GetString(String)

Returns the value mapped by name if it exists, coercing it if necessary, or throws if no such mapping exists.

Has(String)

Returns true if this object has a mapping for name.

IsNull(String)

Returns true if this object has no mapping for name or if it has a mapping whose value is #NULL.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Keys()

Returns an iterator of the String names in this object.

Length()

Returns the number of name/value mappings in this object.

Names()

Returns an array containing the string names in this object.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
NumberToString(Number)

Encodes the number as a JSON string.

Opt(String)

Returns the value mapped by name, or null if no such mapping exists.

OptBoolean(String, Boolean)

Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean, or fallback otherwise.

OptBoolean(String)

Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean, or false otherwise.

OptDouble(String, Double)

Returns the value mapped by name if it exists and is a double or can be coerced to a double, or fallback otherwise.

OptDouble(String)

Returns the value mapped by name if it exists and is a double or can be coerced to a double, or NaN otherwise.

OptInt(String, Int32)

Returns the value mapped by name if it exists and is an int or can be coerced to an int, or fallback otherwise.

OptInt(String)

Returns the value mapped by name if it exists and is an int or can be coerced to an int, or 0 otherwise.

OptJSONArray(String)

Returns the value mapped by name if it exists and is a JSONArray, or null otherwise.

OptJSONObject(String)

Returns the value mapped by name if it exists and is a JSONObject, or null otherwise.

OptLong(String, Int64)

Returns the value mapped by name if it exists and is a long or can be coerced to a long, or fallback otherwise.

OptLong(String)

Returns the value mapped by name if it exists and is a long or can be coerced to a long, or 0 otherwise.

OptString(String, String)

Returns the value mapped by name if it exists, coercing it if necessary, or fallback if no such mapping exists.

OptString(String)

Returns the value mapped by name if it exists, coercing it if necessary, or the empty string if no such mapping exists.

Put(String, Boolean)

Maps name to value, clobbering any existing name/value mapping with the same name.

Put(String, Double)

Maps name to value, clobbering any existing name/value mapping with the same name.

Put(String, Int32)

Maps name to value, clobbering any existing name/value mapping with the same name.

Put(String, Int64)

Maps name to value, clobbering any existing name/value mapping with the same name.

Put(String, Object)

Maps name to value, clobbering any existing name/value mapping with the same name.

PutOpt(String, Object)

Equivalent to put(name, value) when both parameters are non-null; does nothing otherwise.

Quote(String)

Encodes data as a JSON string.

Remove(String)

Removes the named mapping if it exists; does nothing otherwise.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToJSONArray(JSONArray)

Returns an array with the values corresponding to names.

ToString()

Returns a string representation of the object.

(Inherited from Object)
ToString(Int32)

Encodes this object as a human readable JSON string for debugging, such as:

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wrap(Object)

Wraps the given object if necessary.

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to