-
Notifications
You must be signed in to change notification settings - Fork 297
Classes: JSONNode.this[int aIndex]
public virtual JSONNode this[int aIndex] { get; set; }
This indexer is used to access child elements of a JSONArrays. It allows you to access a certain element of that array based on the index. The index is zero based like usual. The valid upper bound is node.Count-1
.
When the node actually represent a JSONObject, using this indexer will access the member values of the object's dictionary by using the Linq function ElementAt. This is not very efficient and not recommended. It's way better to use a foreach loop which will iterate over KeyValuePairs<string, JSONNode>
.
When using an index that is out of bounds (aIndex < 0 || aIndex >= Count) no error will be produced. Instead it will add the value to the end of the array. This is true for both getting and setting the value.So those two lines are equal for JSONArrays:
node[-1] = "Hello World!";
node.Add("Hello World!");
Reading a value that is out of bounds will actually create and return a new JSONLazyCreator instance. This allows to simply chain-create a structure. So doing:
node[-1]["meaning of life"] = 42;
will actually add a new object to the node array and the new object will have a field "meaning of life". For more information see JSONLazyCreator.
For any classes other then JSONArray and JSONObject reading this indexer will always return null
regardless of the index. Setting the indexer for those classes has no effect at all.