json.get: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 29: Line 29:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
   [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b")]
   [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b")]
   [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "XX"]
   [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "XX")]
   [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=12")] [r: json.get(jo, "b", "a")]
  [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b", "XX")]
   [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 1)]
   [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 1)]
   [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 7)]
   [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 7)]
Line 37: Line 38:
Returns
Returns
   44
   44
   empty string ""
   empty string -- ""
   a JSON object -- {"b":44,"a":1}
   a JSON object -- {"b":44,"a":1}
  a JSON object -- {"b":44,"x":""}
   44
   44
   code error -- java.lang.ArrayIndexOutOfBoundsException
   code error -- java.lang.ArrayIndexOutOfBoundsException

Revision as of 07:01, 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.

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.

Examples

  [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "b")]
  [h: jo = json.fromStrProp("a=1; b=44; cat=12")] [r: json.get(jo, "XX")]
  [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=12")] [r: json.get(jo, "b", "XX")]
  [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 1)]
  [h: ja = json.fromList("1,44,12")] [r: json.get(ja, 7)]

Returns

 44
 empty string -- ""
 a JSON object -- {"b":44,"a":1}
 a JSON object -- {"b":44,"x":""}
 44
code error -- java.lang.ArrayIndexOutOfBoundsException

Version Changes