Table of Contents

Class JToken

Namespace
Newtonsoft.Json.Linq
Assembly
Newtonsoft.Json.dll

Represents an abstract JSON token.

public abstract class JToken : IJEnumerable<JToken>, IEnumerable<JToken>, IEnumerable, IJsonLineInfo, ICloneable
Inheritance
JToken
Implements
Derived
Inherited Members
Extension Methods

Properties

EqualityComparer

Gets a comparer that can compare two tokens for value equality.

public static JTokenEqualityComparer EqualityComparer { get; }

Property Value

JTokenEqualityComparer

A JTokenEqualityComparer that can compare two nodes for value equality.

First

Get the first child token of this token.

public virtual JToken First { get; }

Property Value

JToken

A JToken containing the first child token of the JToken.

HasValues

Gets a value indicating whether this token has childen tokens.

public abstract bool HasValues { get; }

Property Value

bool

true if this token has child values; otherwise, false.

this[object]

Gets the JToken with the specified key.

public virtual JToken this[object key] { get; set; }

Parameters

key object

Property Value

JToken

The JToken with the specified key.

Last

Get the last child token of this token.

public virtual JToken Last { get; }

Property Value

JToken

A JToken containing the last child token of the JToken.

Next

Gets the next sibling token of this node.

public JToken Next { get; }

Property Value

JToken

The JToken that contains the next sibling token.

Parent

Gets or sets the parent.

public JContainer Parent { get; }

Property Value

JContainer

The parent.

Previous

Gets the previous sibling token of this node.

public JToken Previous { get; }

Property Value

JToken

The JToken that contains the previous sibling token.

Root

Gets the root JToken of this JToken.

public JToken Root { get; }

Property Value

JToken

The root JToken of this JToken.

Type

Gets the node type for this JToken.

public abstract JTokenType Type { get; }

Property Value

JTokenType

The type.

Methods

AddAfterSelf(object)

Adds the specified content immediately after this token.

public void AddAfterSelf(object content)

Parameters

content object

A content object that contains simple content or a collection of content objects to be added after this token.

AddBeforeSelf(object)

Adds the specified content immediately before this token.

public void AddBeforeSelf(object content)

Parameters

content object

A content object that contains simple content or a collection of content objects to be added before this token.

AfterSelf()

Returns a collection of the sibling tokens after this token, in document order.

public IEnumerable<JToken> AfterSelf()

Returns

IEnumerable<JToken>

A collection of the sibling tokens after this tokens, in document order.

Ancestors()

Returns a collection of the ancestor tokens of this token.

public IEnumerable<JToken> Ancestors()

Returns

IEnumerable<JToken>

A collection of the ancestor tokens of this token.

BeforeSelf()

Returns a collection of the sibling tokens before this token, in document order.

public IEnumerable<JToken> BeforeSelf()

Returns

IEnumerable<JToken>

A collection of the sibling tokens before this token, in document order.

Children()

Returns a collection of the child tokens of this token, in document order.

public virtual JEnumerable<JToken> Children()

Returns

JEnumerable<JToken>

An IEnumerable<T> of JToken containing the child tokens of this JToken, in document order.

Children<T>()

Returns a collection of the child tokens of this token, in document order, filtered by the specified type.

public JEnumerable<T> Children<T>() where T : JToken

Returns

JEnumerable<T>

A JEnumerable<T> containing the child tokens of this JToken, in document order.

Type Parameters

T

The type to filter the child tokens on.

CreateReader()

Creates an JsonReader for this token.

public JsonReader CreateReader()

Returns

JsonReader

An JsonReader that can be used to read this token and its descendants.

DeepClone()

Creates a new instance of the JToken. All child tokens are recursively cloned.

public JToken DeepClone()

Returns

JToken

A new instance of the JToken.

DeepEquals(JToken, JToken)

Compares the values of two tokens, including the values of all descendant tokens.

public static bool DeepEquals(JToken t1, JToken t2)

Parameters

t1 JToken

The first JToken to compare.

t2 JToken

The second JToken to compare.

Returns

bool

true if the tokens are equal; otherwise false.

FromObject(object)

Creates a JToken from an object.

public static JToken FromObject(object o)

Parameters

o object

The object that will be used to create JToken.

Returns

JToken

A JToken with the value of the specified object

FromObject(object, JsonSerializer)

Creates a JToken from an object using the specified JsonSerializer.

public static JToken FromObject(object o, JsonSerializer jsonSerializer)

Parameters

o object

The object that will be used to create JToken.

jsonSerializer JsonSerializer

The JsonSerializer that will be used when reading the object.

Returns

JToken

A JToken with the value of the specified object

Load(JsonReader)

Creates a JToken from a JsonReader.

public static JToken Load(JsonReader reader)

Parameters

reader JsonReader

An JsonReader positioned at the token to read into this JToken.

Returns

JToken

An JToken that contains the token and its descendant tokens that were read from the reader. The runtime type of the token is determined by the token type of the first token encountered in the reader.

Parse(string)

Load a JToken from a string that contains JSON.

public static JToken Parse(string json)

Parameters

json string

A string that contains JSON.

Returns

JToken

A JToken populated from the string that contains JSON.

ReadFrom(JsonReader)

Creates a JToken from a JsonReader.

public static JToken ReadFrom(JsonReader reader)

Parameters

reader JsonReader

An JsonReader positioned at the token to read into this JToken.

Returns

JToken

An JToken that contains the token and its descendant tokens that were read from the reader. The runtime type of the token is determined by the token type of the first token encountered in the reader.

Remove()

Removes this token from its parent.

public void Remove()

Replace(JToken)

Replaces this token with the specified token.

public void Replace(JToken value)

Parameters

value JToken

The value.

SelectToken(string)

Selects the token that matches the object path.

public JToken SelectToken(string path)

Parameters

path string

The object path from the current JToken to the JToken to be returned. This must be a string of property names or array indexes separated by periods, such as

Tables[0].DefaultView[0].Price
in C# or
Tables(0).DefaultView(0).Price
in Visual Basic.

Returns

JToken

The JToken that matches the object path or a null reference if no matching token is found.

SelectToken(string, bool)

Selects the token that matches the object path.

public JToken SelectToken(string path, bool errorWhenNoMatch)

Parameters

path string

The object path from the current JToken to the JToken to be returned. This must be a string of property names or array indexes separated by periods, such as

Tables[0].DefaultView[0].Price
in C# or
Tables(0).DefaultView(0).Price
in Visual Basic.
errorWhenNoMatch bool

A flag to indicate whether an error should be thrown if no token is found.

Returns

JToken

The JToken that matches the object path.

ToObject<T>()

Creates the specified .NET type from the JToken.

public T ToObject<T>()

Returns

T

The new object created from the JSON value.

Type Parameters

T

ToObject<T>(JsonSerializer)

Creates the specified .NET type from the JToken using the specified JsonSerializer.

public T ToObject<T>(JsonSerializer jsonSerializer)

Parameters

jsonSerializer JsonSerializer

The JsonSerializer that will be used when creating the object.

Returns

T

The new object created from the JSON value.

Type Parameters

T

ToString()

Returns the indented JSON for this token.

public override string ToString()

Returns

string

The indented JSON for this token.

ToString(Formatting, params JsonConverter[])

Returns the JSON for this token using the given formatting and converters.

public string ToString(Formatting formatting, params JsonConverter[] converters)

Parameters

formatting Formatting

Indicates how the output is formatted.

converters JsonConverter[]

A collection of JsonConverter which will be used when writing the token.

Returns

string

The JSON for this token using the given formatting and converters.

Value<T>(object)

Gets the JToken with the specified key converted to the specified type.

public virtual T Value<T>(object key)

Parameters

key object

The token key.

Returns

T

The converted token value.

Type Parameters

T

The type to convert the token to.

Values<T>()

Returns a collection of the child values of this token, in document order.

public virtual IEnumerable<T> Values<T>()

Returns

IEnumerable<T>

A IEnumerable<T> containing the child values of this JToken, in document order.

Type Parameters

T

The type to convert the values to.

WriteTo(JsonWriter, params JsonConverter[])

Writes this token to a JsonWriter.

public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters)

Parameters

writer JsonWriter

A JsonWriter into which this method will write.

converters JsonConverter[]

A collection of JsonConverter which will be used when writing the token.

Operators

explicit operator bool(JToken)

Performs an explicit conversion from JToken to bool.

public static explicit operator bool(JToken value)

Parameters

value JToken

The value.

Returns

bool

The result of the conversion.

explicit operator byte[](JToken)

Performs an explicit conversion from JToken to byte[].

public static explicit operator byte[](JToken value)

Parameters

value JToken

The value.

Returns

byte[]

The result of the conversion.

explicit operator DateTime(JToken)

Performs an explicit conversion from JToken to DateTime.

public static explicit operator DateTime(JToken value)

Parameters

value JToken

The value.

Returns

DateTime

The result of the conversion.

explicit operator DateTimeOffset(JToken)

Performs an explicit conversion from JToken to DateTimeOffset.

public static explicit operator DateTimeOffset(JToken value)

Parameters

value JToken

The value.

Returns

DateTimeOffset

The result of the conversion.

explicit operator decimal(JToken)

Performs an explicit conversion from JToken to decimal.

public static explicit operator decimal(JToken value)

Parameters

value JToken

The value.

Returns

decimal

The result of the conversion.

explicit operator double(JToken)

Performs an explicit conversion from JToken to double.

public static explicit operator double(JToken value)

Parameters

value JToken

The value.

Returns

double

The result of the conversion.

explicit operator short(JToken)

Performs an explicit conversion from JToken to short.

public static explicit operator short(JToken value)

Parameters

value JToken

The value.

Returns

short

The result of the conversion.

explicit operator int(JToken)

Performs an explicit conversion from JToken to int.

public static explicit operator int(JToken value)

Parameters

value JToken

The value.

Returns

int

The result of the conversion.

explicit operator long(JToken)

Performs an explicit conversion from JToken to long.

public static explicit operator long(JToken value)

Parameters

value JToken

The value.

Returns

long

The result of the conversion.

explicit operator bool?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator bool?(JToken value)

Parameters

value JToken

The value.

Returns

bool?

The result of the conversion.

explicit operator DateTimeOffset?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator DateTimeOffset?(JToken value)

Parameters

value JToken

The value.

Returns

DateTimeOffset?

The result of the conversion.

explicit operator DateTime?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator DateTime?(JToken value)

Parameters

value JToken

The value.

Returns

DateTime?

The result of the conversion.

explicit operator decimal?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator decimal?(JToken value)

Parameters

value JToken

The value.

Returns

decimal?

The result of the conversion.

explicit operator double?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator double?(JToken value)

Parameters

value JToken

The value.

Returns

double?

The result of the conversion.

explicit operator short?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator short?(JToken value)

Parameters

value JToken

The value.

Returns

short?

The result of the conversion.

explicit operator int?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator int?(JToken value)

Parameters

value JToken

The value.

Returns

int?

The result of the conversion.

explicit operator long?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator long?(JToken value)

Parameters

value JToken

The value.

Returns

long?

The result of the conversion.

explicit operator float?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

public static explicit operator float?(JToken value)

Parameters

value JToken

The value.

Returns

float?

The result of the conversion.

explicit operator ushort?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

[CLSCompliant(false)]
public static explicit operator ushort?(JToken value)

Parameters

value JToken

The value.

Returns

ushort?

The result of the conversion.

explicit operator uint?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

[CLSCompliant(false)]
public static explicit operator uint?(JToken value)

Parameters

value JToken

The value.

Returns

uint?

The result of the conversion.

explicit operator ulong?(JToken)

Performs an explicit conversion from JToken to Nullable<T>.

[CLSCompliant(false)]
public static explicit operator ulong?(JToken value)

Parameters

value JToken

The value.

Returns

ulong?

The result of the conversion.

explicit operator float(JToken)

Performs an explicit conversion from JToken to float.

public static explicit operator float(JToken value)

Parameters

value JToken

The value.

Returns

float

The result of the conversion.

explicit operator string(JToken)

Performs an explicit conversion from JToken to string.

public static explicit operator string(JToken value)

Parameters

value JToken

The value.

Returns

string

The result of the conversion.

explicit operator ushort(JToken)

Performs an explicit conversion from JToken to ushort.

[CLSCompliant(false)]
public static explicit operator ushort(JToken value)

Parameters

value JToken

The value.

Returns

ushort

The result of the conversion.

explicit operator uint(JToken)

Performs an explicit conversion from JToken to uint.

[CLSCompliant(false)]
public static explicit operator uint(JToken value)

Parameters

value JToken

The value.

Returns

uint

The result of the conversion.

explicit operator ulong(JToken)

Performs an explicit conversion from JToken to ulong.

[CLSCompliant(false)]
public static explicit operator ulong(JToken value)

Parameters

value JToken

The value.

Returns

ulong

The result of the conversion.

implicit operator JToken(bool)

Performs an implicit conversion from bool to JToken.

public static implicit operator JToken(bool value)

Parameters

value bool

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(byte[])

Performs an implicit conversion from byte[] to JToken.

public static implicit operator JToken(byte[] value)

Parameters

value byte[]

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(DateTime)

Performs an implicit conversion from DateTime to JToken.

public static implicit operator JToken(DateTime value)

Parameters

value DateTime

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(DateTimeOffset)

Performs an implicit conversion from DateTimeOffset to JToken.

public static implicit operator JToken(DateTimeOffset value)

Parameters

value DateTimeOffset

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(decimal)

Performs an implicit conversion from decimal to JToken.

public static implicit operator JToken(decimal value)

Parameters

value decimal

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(double)

Performs an implicit conversion from double to JToken.

public static implicit operator JToken(double value)

Parameters

value double

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(short)

Performs an implicit conversion from short to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(short value)

Parameters

value short

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(int)

Performs an implicit conversion from int to JToken.

public static implicit operator JToken(int value)

Parameters

value int

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(long)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(long value)

Parameters

value long

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(bool?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(bool? value)

Parameters

value bool?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(DateTimeOffset?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(DateTimeOffset? value)

Parameters

value DateTimeOffset?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(DateTime?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(DateTime? value)

Parameters

value DateTime?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(decimal?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(decimal? value)

Parameters

value decimal?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(double?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(double? value)

Parameters

value double?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(short?)

Performs an implicit conversion from Nullable<T> to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(short? value)

Parameters

value short?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(int?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(int? value)

Parameters

value int?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(long?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(long? value)

Parameters

value long?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(float?)

Performs an implicit conversion from Nullable<T> to JToken.

public static implicit operator JToken(float? value)

Parameters

value float?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(ushort?)

Performs an implicit conversion from Nullable<T> to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(ushort? value)

Parameters

value ushort?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(uint?)

Performs an implicit conversion from Nullable<T> to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(uint? value)

Parameters

value uint?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(ulong?)

Performs an implicit conversion from Nullable<T> to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(ulong? value)

Parameters

value ulong?

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(float)

Performs an implicit conversion from float to JToken.

public static implicit operator JToken(float value)

Parameters

value float

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(string)

Performs an implicit conversion from string to JToken.

public static implicit operator JToken(string value)

Parameters

value string

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(ushort)

Performs an implicit conversion from ushort to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(ushort value)

Parameters

value ushort

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(uint)

Performs an implicit conversion from uint to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(uint value)

Parameters

value uint

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.

implicit operator JToken(ulong)

Performs an implicit conversion from ulong to JToken.

[CLSCompliant(false)]
public static implicit operator JToken(ulong value)

Parameters

value ulong

The value to create a JValue from.

Returns

JToken

The JValue initialized with the specified value.