Json.JsonElement

Represent each building block of JSON data

WSupported on Windows
USupported on Unix
VSupported on OpenVMS

 

namespace Json
public class JsonElement

The JsonElement class represents a specific JSON value within a JsonDocument.

Methods

GetProperty

public GetProperty(propertyname), @JsonElement 

Returns a JsonElement that represents the value of a required property specified by propertyname (@string). If the property is defined multiple times, GetProperty looks for the last definition.

TryGetProperty

public TryGetProperty(propertyname, value), boolean

Looks for propertyname (@string) in the current element and returns a value of true to indicate propertyname was found or false to indicate it was not found. If propertyname is found, its value is assigned to value (@JsonElement). Value should not be used if TryGetProperty returns false. If the property is defined multiple times, TryGetProperty looks for the last definition.

GetArrayLength

public GetArrayLength(), int

Returns the length of the represented array (i.e., the number of values in the current array value).

GetBoolean

public GetBoolean(), boolean

Returns the boolean value of the element.

GetByte

public GetByte(), i1

Returns the i1 value of the element.

GetStringFromBase64

public GetStringFromBase64(), @string

Returns the decoded string value of the element.

GetDecimal

public GetDecimal(), decimal

Returns the equivalent implied-decimal (i.e., decimal data type) value of the element for compatibility with .NET, which returns System.Decimal.

GetInt16

public GetInt16(), short

Returns the i2 value of the element.

GetInt32

public GetInt32(), int

Returns the i4 value of the element.

GetInt64

public GetInt64(), i8

Returns the i8 value of the element.

GetString

public GetString(), @string

Returns the string value of the element.

Properties

ValueKind

public ValueKind

Returns the type of the current JSON value. ValueKind doesn’t differentiate between the sized integer types and decimal. The int versus decimal decision is an optimization that you as the developer should make where appropriate.

Indexer

public Indexer(index)

Returns the value for the requested index if the current value is an array. The index is zero-based.

Discussion

If a JsonElement represents the following JSON:

{"MyProperty": "MyPropertyValue"}

calling GetProperty(“MyProperty”) will return a JsonElement with the ValueKind property set to JsonValueKind.String. Calling GetString on the resulting JsonElement will return the System.String value of the element.

Examples

if(element.ValueKind == JsonValueKind.String) then 
Console.WriteLine(element.GetString())
else if(element.ValueKind == JsonValueKind.Number) then
Console.WriteLine(element.GetDecimal())
else if(element.ValueKind == JsonValueKind.Object)
element.GetProperty("SomeInterestingObjectPropertyName") ;Returns requested JsonElement
else if(element.ValueKind == JsonValueKind.Array)
element[desiredZeroBasedArrayElementIndex] ;Returns requested JsonElement

Also see Understanding the Synergy JSON API for a complete example.