json.get: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 24: Line 24:
{{param|key|The name of a field that should be returned. This parameter can exist more than once, if it does then a [[JSON Object]] is returned with all the specified elements.}}
{{param|key|The name of a field that should be returned. This parameter can exist more than once, if it does then a [[JSON Object]] is returned with all the specified elements.}}


Negative numbers can be used as the offsets from the end of the array, {{code|-1}} is the last element in the array, {{code|-2}} is the second to last, and so on.  If the {{code|end}} index is smaller than the {{code|start}} index then the array slice is returned in reverse.
When extracting slices: Negative numbers can be used as the offsets from the end of the array, {{code|-1}} is the last element in the array, {{code|-2}} is the second to last, and so on.  If the {{code|end}} index is smaller than the {{code|start}} index then the array slice is returned in reverse. (Negative numbers do not work with {{param|index|The numerical index of the element you want returned.}}


|examples=
|examples=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
   a) [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b")]
   a) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "b")] <br>
   b) [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "XX")]
   b) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "XX")] <br>
   c) [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b", "a")]
   c) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "cat", "a")] <br>
   d) [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b", "XX")]
   d) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "b", "XX")] <br>
   e) [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 1)]
   e) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 1)] <br>
   f) [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 7)]
   f) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 2)] <br>
   g) [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, 1, 3)]
   g) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 1, 2)] <br>
  h) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 0, -1)] <br>
  i) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 2, 0)] <br>
  j) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, -1, 0)] <br>
  ERROR a) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, -1)] <br>
  ERROR b) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 3)] <br>
 
</source>
</source>


Returns
Returns
   a) 44
   a) 44 -- a value
   b) empty string as XX does not exist -- ""
   b)   -- empty string "", as XX does not exist
   c) a JSON object -- {"b":44,"a":1}
   c) {"cat":"meow","a":1} -- a JSON object
   d) a JSON object -- {"b":44,"XX":""}
   d) {"b":44,"XX":""} -- a JSON object
   e) 44
   e) 44 -- a value
   f) code error -- java.lang.ArrayIndexOutOfBoundsException
   f) meow -- a value
   g) a JSON object -- {"3":"","1":""}
   g) [1,44,"meow"] -- an array slice of 1,2
  h) [1,44,"meow"] -- an array slice from 0..end
  i) ["meow",44,1] -- an array slice from 2..1  
  j) ["meow",44] -- an array slice from end..1
 
  ERROR a) -- java.lang.ArrayIndexOutOfBoundsException, negatives unfortunately crash when not using slices with start..end indexes.
  ERROR a) -- java.lang.ArrayIndexOutOfBoundsException, the last valid index is 2
|changes=
|changes=
{{change|1.3b51|Added ability to return [[JSON Array]] slices.}}
{{change|1.3b51|Added ability to return [[JSON Array]] slices.}}

Revision as of 07:58, 31 December 2010

 This article is a stub, you can help the RPTools Wiki project by contributing content to expand this article.
 This article needs: Examples for the new functionality.

json.get() Function

Introduced in version 1.3b49
Returns the value in a JSON Array at the specified index, returns a slice of a JSON Array from the specified indexes, or returns the value from JSON Object for the specified key.

Usage

json.get(array, index)
json.get(array, start, end)
json.get(object, key, key, ...)

Parameters

  • array - The JSON Array to retrieve the element from.
  • index - The numerical index of the element you want returned.
  • start - The starting index of the element you wish the slice to begin at.
  • end - The ending index of the element you wish the slice to end at.
  • object - The JSON Object to retrieve the element from.
  • key - The name of a field that should be returned. This parameter can exist more than once, if it does then a JSON Object is returned with all the specified elements.

When extracting slices: Negative numbers can be used as the offsets from the end of the array, -1 is the last element in the array, -2 is the second to last, and so on. If the end index is smaller than the start index then the array slice is returned in reverse. (Negative numbers do not work with

  • index - The numerical index of the element you want returned.

Examples

  a) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "b")] <br>
  b) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "XX")] <br>
  c) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "cat", "a")] <br>
  d) [h: jo = json.fromStrProp("a=1; b=44; cat=meow")] [r: json.get(jo, "b", "XX")] <br>
  e) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 1)] <br>
  f) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 2)] <br>
  g) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 1, 2)] <br>
  h) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 0, -1)] <br>
  i) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 2, 0)] <br> 
  j) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, -1, 0)] <br>
  ERROR a) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, -1)] <br>
  ERROR b) [h: ja = json.fromList("1,44,meow")] [r: json.get(ja, 3)] <br>

Returns

 a) 44 -- a value
 b)    -- empty string "", as XX does not exist
 c) {"cat":"meow","a":1} -- a JSON object
 d) {"b":44,"XX":""} -- a JSON object
 e) 44 -- a value
 f) meow -- a value
 g) [1,44,"meow"] -- an array slice of 1,2
 h) [1,44,"meow"] -- an array slice from 0..end
 i) ["meow",44,1] -- an array slice from 2..1 
 j) ["meow",44] -- an array slice from end..1
 ERROR a) -- java.lang.ArrayIndexOutOfBoundsException, negatives unfortunately crash when not using slices with start..end indexes. 
ERROR a) -- java.lang.ArrayIndexOutOfBoundsException, the last valid index is 2

Version Changes