JsonObject JsonObject JsonObject Class

Represents a JSON object containing a collection of name and JsonValue pairs.

JsonObject is an activatable class that implements JsonValue and the IMap<K, V> interface such that its name/value pairs can be manipulated like a dictionary. When there are values with duplicated names, the last name/value pair will be stored.

Syntax

Declaration

public sealed class JsonObjectpublic sealed class JsonObjectPublic NotInheritable Class JsonObject

Remarks

For an example of how these class methods are used to parse an object from a JSON string and convert it into a JsonObject object, update the name/value pairs the object contains, and then serialize the updated JsonObject object as a JSON string, see Using JavaScript Object Notation (JSON).

Constructors summary

Initializes a new instance of a JsonObject that represents a JSON object containing a collection of name and JsonValue pairs.

Properties summary

Gets the size (count) of the collection.

Gets the type of the encapsulated JSON value. Possible types are defined by JsonValueType.

Methods summary

Removes all items from the collection.

Returns the iterator for iteration over the key/value pairs in the JsonObject.

Returns a JsonArray if the ValueType of the encapsulated value is Array.

Gets a Boolean if the ValueType of the encapsulated value is Boolean.

Gets the JsonArray value with the specified name.

Gets the @Windows.Data.Json.JsonArray.#ctor value with the specified name, or the provided default value if no such named value is found.

Gets the Boolean value with the specified name.

Gets the Boolean value with the specified name, or the provided default value if no such named value is found.

Gets the number value (a Double) with the specified name.

Gets the number value (a Double) with the specified name, or the provided default value if no such named value is found.

Gets the JsonObject value with the specified name.

Gets the JsonObject value with the specified name, or the provided default value if no such named value is found.

Gets the String value with the specified name.

Gets the String value with the specified name, or the provided default value if no such named value is found.

Gets the JsonValue value with the specified name.

Gets the specified JsonValue value, or the provided default value if no such named value is found.

Gets the number (a Double) if the ValueType of the encapsulated value is Number.

Gets the JsonObject if the ValueType of the encapsulated value is Object.

Gets the String if the ValueType of the encapsulated value is String.

Gets a JsonObject view.

Indicates whether the JsonObject has an entry with the requested key.

Adds a new entry to the JsonObject.

Gets the JSON value associated with the supplied key, if an entry with that key exists.

Parses the specified JSON string that represents a JsonObject into a JSON value.

Note

This will throw an exception if the provided JSON string is not valid. Use of TryParse(System.String,Windows.Data.Json.JsonObject) is a recommended alternative. A TryParse(System.String,Windows.Data.Json.JsonObject) call will return a boolean value to indicate success or failure and, if successful, the resultant JsonObject.

Removes a specific item from the JsonObject.

Sets the value of the first JsonValue object with the specified name to the specified value. If no such object is found, a new name and JsonValue pair is inserted into the JSON object.

Retrieves the JSON representation of the encapsulated value.

Retrieves the JSON representation of the encapsulated value.

Parses the specified string that represents a JSON object into a JsonObject.

Constructors

  • JsonObject()
    JsonObject()
    JsonObject()
    JsonObject()

    Initializes a new instance of a JsonObject that represents a JSON object containing a collection of name and JsonValue pairs.

    public JsonObject()public JsonObject()Public Function JsonObject() As

Properties

  • Size
    Size
    Size
    Size

    Gets the size (count) of the collection.

    public uint Size { get; }public uint Size { get; }Public ReadOnly Property Size As uint

    Property Value

    • uint
      uint
      uint

      The count of items in the collection.

  • ValueType
    ValueType
    ValueType
    ValueType

    Gets the type of the encapsulated JSON value. Possible types are defined by JsonValueType.

    public JsonValueType ValueType { get; }public JsonValueType ValueType { get; }Public ReadOnly Property ValueType As JsonValueType

    Property Value

Methods

  • Clear()
    Clear()
    Clear()
    Clear()

    Removes all items from the collection.

    public void Clear()public void Clear()Public Function Clear() As void
  • First()
    First()
    First()
    First()

    Returns the iterator for iteration over the key/value pairs in the JsonObject.

    public IIterator<IJsonValue>> First()public IIterator<IJsonValue>> First()Public Function First() As IIterator( Of IJsonValue )

    Returns

    • The iterator object. The iterator's current position is at the 0-index position, or at the array end if the array is empty.

  • GetArray()
    GetArray()
    GetArray()
    GetArray()

    Returns a JsonArray if the ValueType of the encapsulated value is Array.

    public JsonArray GetArray()public JsonArray GetArray()Public Function GetArray() As JsonArray

    Returns

  • GetBoolean()
    GetBoolean()
    GetBoolean()
    GetBoolean()

    Gets a Boolean if the ValueType of the encapsulated value is Boolean.

    public bool GetBoolean()public bool GetBoolean()Public Function GetBoolean() As bool

    Returns

    • bool
      bool
      bool

      The Boolean value.

  • GetNamedArray(System.String)
    GetNamedArray(System.String)
    GetNamedArray(System.String)
    GetNamedArray(System.String)

    Gets the JsonArray value with the specified name.

    public JsonArray GetNamedArray(System.String)public JsonArray GetNamedArray(System.String)Public Function GetNamedArray(System.String) As JsonArray

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    Returns

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name found is not a JsonArray type or the name is not found.

  • GetNamedArray(System.String,Windows.Data.Json.JsonArray)
    GetNamedArray(System.String,Windows.Data.Json.JsonArray)
    GetNamedArray(System.String,Windows.Data.Json.JsonArray)
    GetNamedArray(System.String,Windows.Data.Json.JsonArray)

    Gets the @Windows.Data.Json.JsonArray.#ctor value with the specified name, or the provided default value if no such named value is found.

    public JsonArray GetNamedArray(System.String,Windows.Data.Json.JsonArray)public JsonArray GetNamedArray(System.String,Windows.Data.Json.JsonArray)Public Function GetNamedArray(System.String,Windows.Data.Json.JsonArray) As JsonArray

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    • defaultValue

      The default value to use if the JSON property is not found.

    Returns

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name is not found.

  • GetNamedBoolean(System.String)
    GetNamedBoolean(System.String)
    GetNamedBoolean(System.String)
    GetNamedBoolean(System.String)

    Gets the Boolean value with the specified name.

    public bool GetNamedBoolean(System.String)public bool GetNamedBoolean(System.String)Public Function GetNamedBoolean(System.String) As bool

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    Returns

    • bool
      bool
      bool

      The Boolean with the specified name.

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name found is not a Boolean type or the name is not found.

  • GetNamedBoolean(System.String,System.Boolean)
    GetNamedBoolean(System.String,System.Boolean)
    GetNamedBoolean(System.String,System.Boolean)
    GetNamedBoolean(System.String,System.Boolean)

    Gets the Boolean value with the specified name, or the provided default value if no such named value is found.

    public bool GetNamedBoolean(System.String,System.Boolean)public bool GetNamedBoolean(System.String,System.Boolean)Public Function GetNamedBoolean(System.String,System.Boolean) As bool

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    • defaultValue
      System.Boolean
      System.Boolean
      System.Boolean

      The default value to use if the JSON property is not found.

    Returns

    • bool
      bool
      bool

      The Boolean with the specified name, or if this value wasn't found, the defaultValue is returned.

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name is not found.

  • GetNamedNumber(System.String)
    GetNamedNumber(System.String)
    GetNamedNumber(System.String)
    GetNamedNumber(System.String)

    Gets the number value (a Double) with the specified name.

    public double GetNamedNumber(System.String)public double GetNamedNumber(System.String)Public Function GetNamedNumber(System.String) As double

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    Returns

    • double
      double
      double

      The Double with the specified name.

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name found is not a Double type or the name is not found.

  • GetNamedNumber(System.String,System.Double)
    GetNamedNumber(System.String,System.Double)
    GetNamedNumber(System.String,System.Double)
    GetNamedNumber(System.String,System.Double)

    Gets the number value (a Double) with the specified name, or the provided default value if no such named value is found.

    public double GetNamedNumber(System.String,System.Double)public double GetNamedNumber(System.String,System.Double)Public Function GetNamedNumber(System.String,System.Double) As double

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    • defaultValue
      System.Double
      System.Double
      System.Double

      The default value to use if the JSON property is not found.

    Returns

    • double
      double
      double

      The Double with the specified name, or if this value wasn't found, the defaultValue is returned.

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name is not found.

  • GetNamedObject(System.String)
    GetNamedObject(System.String)
    GetNamedObject(System.String)
    GetNamedObject(System.String)

    Gets the JsonObject value with the specified name.

    public JsonObject GetNamedObject(System.String)public JsonObject GetNamedObject(System.String)Public Function GetNamedObject(System.String) As JsonObject

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    Returns

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name found is not a JsonObject type or the name is not found.

  • GetNamedObject(System.String,Windows.Data.Json.JsonObject)
    GetNamedObject(System.String,Windows.Data.Json.JsonObject)
    GetNamedObject(System.String,Windows.Data.Json.JsonObject)
    GetNamedObject(System.String,Windows.Data.Json.JsonObject)

    Gets the JsonObject value with the specified name, or the provided default value if no such named value is found.

    public JsonObject GetNamedObject(System.String,Windows.Data.Json.JsonObject)public JsonObject GetNamedObject(System.String,Windows.Data.Json.JsonObject)Public Function GetNamedObject(System.String,Windows.Data.Json.JsonObject) As JsonObject

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    • defaultValue

      The default value to use if the JSON property is not found.

    Returns

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name is not found.

  • GetNamedString(System.String)
    GetNamedString(System.String)
    GetNamedString(System.String)
    GetNamedString(System.String)

    Gets the String value with the specified name.

    public string GetNamedString(System.String)public string GetNamedString(System.String)Public Function GetNamedString(System.String) As string

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    Returns

    • string
      string
      string

      The String value with the specified name.

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name found is not a String type or the name is not found.

  • GetNamedString(System.String,System.String)
    GetNamedString(System.String,System.String)
    GetNamedString(System.String,System.String)
    GetNamedString(System.String,System.String)

    Gets the String value with the specified name, or the provided default value if no such named value is found.

    public string GetNamedString(System.String,System.String)public string GetNamedString(System.String,System.String)Public Function GetNamedString(System.String,System.String) As string

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    • defaultValue
      System.String
      System.String
      System.String

      The default value to use if the JSON property is not found.

    Returns

    • string
      string
      string

      The String with the specified name, or if this value wasn't found, the defaultValue is returned.

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name is not found.

  • GetNamedValue(System.String)
    GetNamedValue(System.String)
    GetNamedValue(System.String)
    GetNamedValue(System.String)

    Gets the JsonValue value with the specified name.

    public JsonValue GetNamedValue(System.String)public JsonValue GetNamedValue(System.String)Public Function GetNamedValue(System.String) As JsonValue

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    Returns

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name found is not a JsonValue type or the name is not found.

  • GetNamedValue(System.String,Windows.Data.Json.JsonValue)
    GetNamedValue(System.String,Windows.Data.Json.JsonValue)
    GetNamedValue(System.String,Windows.Data.Json.JsonValue)
    GetNamedValue(System.String,Windows.Data.Json.JsonValue)

    Gets the specified JsonValue value, or the provided default value if no such named value is found.

    public JsonValue GetNamedValue(System.String,Windows.Data.Json.JsonValue)public JsonValue GetNamedValue(System.String,Windows.Data.Json.JsonValue)Public Function GetNamedValue(System.String,Windows.Data.Json.JsonValue) As JsonValue

    Parameters

    • name
      System.String
      System.String
      System.String

      The name.

    • defaultValue

      The default value to use if the JSON property is not found.

    Returns

    Remarks

    This method should always used with a try/catch block because it throws an exception if the name is not found.

  • GetNumber()
    GetNumber()
    GetNumber()
    GetNumber()

    Gets the number (a Double) if the ValueType of the encapsulated value is Number.

    public double GetNumber()public double GetNumber()Public Function GetNumber() As double

    Returns

    • double
      double
      double

      The number value.

  • GetObject()
    GetObject()
    GetObject()
    GetObject()

    Gets the JsonObject if the ValueType of the encapsulated value is Object.

    public JsonObject GetObject()public JsonObject GetObject()Public Function GetObject() As JsonObject

    Returns

  • GetString()
    GetString()
    GetString()
    GetString()

    Gets the String if the ValueType of the encapsulated value is String.

    public string GetString()public string GetString()Public Function GetString() As string

    Returns

    • string
      string
      string

      The String value.

  • GetView()
    GetView()
    GetView()
    GetView()

    Gets a JsonObject view.

    public IMapView<IJsonValue> GetView()public IMapView<IJsonValue> GetView()Public Function GetView() As IMapView( Of IJsonValue )

    Returns

  • HasKey(System.String)
    HasKey(System.String)
    HasKey(System.String)
    HasKey(System.String)

    Indicates whether the JsonObject has an entry with the requested key.

    public bool HasKey(System.String)public bool HasKey(System.String)Public Function HasKey(System.String) As bool

    Parameters

    • key
      System.String
      System.String
      System.String

      The requested key.

    Returns

    • bool
      bool
      bool

      true if the JsonObject has an entry with the requested key; otherwise, false.

  • Insert(System.String,Windows.Data.Json.IJsonValue)
    Insert(System.String,Windows.Data.Json.IJsonValue)
    Insert(System.String,Windows.Data.Json.IJsonValue)
    Insert(System.String,Windows.Data.Json.IJsonValue)

    Adds a new entry to the JsonObject.

    public bool Insert(System.String,Windows.Data.Json.IJsonValue)public bool Insert(System.String,Windows.Data.Json.IJsonValue)Public Function Insert(System.String,Windows.Data.Json.IJsonValue) As bool

    Parameters

    Returns

    • bool
      bool
      bool

      true if calling the method replaced a value that already existed for the key; false if calling the method defined a new key.

  • Lookup(System.String)
    Lookup(System.String)
    Lookup(System.String)
    Lookup(System.String)

    Gets the JSON value associated with the supplied key, if an entry with that key exists.

    public IJsonValue Lookup(System.String)public IJsonValue Lookup(System.String)Public Function Lookup(System.String) As IJsonValue

    Parameters

    • key
      System.String
      System.String
      System.String

      The requested key.

    Returns

  • Parse(System.String)
    Parse(System.String)
    Parse(System.String)
    Parse(System.String)

    Parses the specified JSON string that represents a JsonObject into a JSON value.

    Note

    This will throw an exception if the provided JSON string is not valid. Use of TryParse(System.String,Windows.Data.Json.JsonObject) is a recommended alternative. A TryParse(System.String,Windows.Data.Json.JsonObject) call will return a boolean value to indicate success or failure and, if successful, the resultant JsonObject.

    public static JsonObject Parse(System.String)public static JsonObject Parse(System.String)Public Static Function Parse(System.String) As JsonObject

    Parameters

    • input
      System.String
      System.String
      System.String

      The specified JSON string.

    Returns

    Remarks

    This method may only be called when the ValueType is Null. If the value contained in the specified JSON string is a String, a Number or a Boolean, the ValueType will be updated and the actual value can then be accessed through the GetString(), GetNumber() or GetBoolean() methods. If Parse(System.String) fails due to an invalid JSON string or resource allocation errors, the ValueType will not be changed.

    When implemented on the subclass @Windows.Data.Json.JsonObject.#ctor, this method requires the specified input string to be a valid JSON object value. When implemented on the subclass @Windows.Data.Json.JsonArray.#ctor, this method requires the specified input string to be a valid JSON array value. The original object state will be overwritten when @Windows.Data.Json.JsonObject.#ctor.@Windows.Data.Json.JsonValue.Parse(System.String) or @Windows.Data.Json.JsonArray.#ctor.@Windows.Data.Json.JsonValue.Parse(System.String) succeeds. If Parse(System.String) fails, the object state remains intact.

  • Remove(System.String)
    Remove(System.String)
    Remove(System.String)
    Remove(System.String)

    Removes a specific item from the JsonObject.

    public void Remove(System.String)public void Remove(System.String)Public Function Remove(System.String) As void

    Parameters

    • key
      System.String
      System.String
      System.String

      The key of the item to remove.

  • SetNamedValue(System.String,Windows.Data.Json.IJsonValue)
    SetNamedValue(System.String,Windows.Data.Json.IJsonValue)
    SetNamedValue(System.String,Windows.Data.Json.IJsonValue)
    SetNamedValue(System.String,Windows.Data.Json.IJsonValue)

    Sets the value of the first JsonValue object with the specified name to the specified value. If no such object is found, a new name and JsonValue pair is inserted into the JSON object.

    public void SetNamedValue(System.String,Windows.Data.Json.IJsonValue)public void SetNamedValue(System.String,Windows.Data.Json.IJsonValue)Public Function SetNamedValue(System.String,Windows.Data.Json.IJsonValue) As void

    Parameters

  • Stringify()
    Stringify()
    Stringify()
    Stringify()

    Retrieves the JSON representation of the encapsulated value.

    public string Stringify()public string Stringify()Public Function Stringify() As string

    Returns

    • string
      string
      string

      A JSON representation of the encapsulated value.

    Remarks

    If the ValueType is String, then the returned string will be properly escaped and double quoted. This method is used for serializing the encapsulated value into a valid JSON format.

  • ToString()
    ToString()
    ToString()
    ToString()

    Retrieves the JSON representation of the encapsulated value.

    public string ToString()public string ToString()Public Function ToString() As string

    Returns

    • string
      string
      string

      A JSON representation of the encapsulated value.

    Remarks

    If the ValueType is String, then the returned string will be properly escaped and double quoted. This method is used for serializing the encapsulated value into a valid JSON format.

  • TryParse(System.String,Windows.Data.Json.JsonObject)
    TryParse(System.String,Windows.Data.Json.JsonObject)
    TryParse(System.String,Windows.Data.Json.JsonObject)
    TryParse(System.String,Windows.Data.Json.JsonObject)

    Parses the specified string that represents a JSON object into a JsonObject.

    public static bool TryParse(System.String,Windows.Data.Json.JsonObject)public static bool TryParse(System.String,Windows.Data.Json.JsonObject)Public Static Function TryParse(System.String,Windows.Data.Json.JsonObject) As bool

    Parameters

    Returns

    • bool
      bool
      bool

      True if the JsonValue object was successfully created; otherwise, false.

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ActivatableAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.DualApiPartitionAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.WebHostHiddenAttribute
Windows.Foundation.Metadata.ThreadingAttribute

Details

Assembly

Windows.Data.Json.dll