类 JsonTool

所有已实现的接口:
Serializable, Iterable

@DefaultKey("json") @InvalidScope("session") public class JsonTool extends ImportSupport implements Iterable, Serializable

Tool which can parse a JSON file.

Usage:

  • $json.parse(JSON string)
  • $json.read(file or classpath resource)
  • $json.fetch(URL)

Configuration parameters:

  • resource=file or classpath resource
  • source=URL

Example configuration:

 <tools>
   <toolbox scope="request">
     <tool class="org.apache.velocity.tools.generic.JsonTool"
              key="foo" resource="doc.xml"/>
   </toolbox>
 </tools>
 
从以下版本开始:
VelocityTools 3.0
版本:
$Id:$
作者:
Claude Brisson
另请参阅:
  • 字段详细资料

    • importSupport

      protected transient ImportSupport importSupport
      ImportSupport utility which provides underlying i/o
  • 构造器详细资料

    • JsonTool

      public JsonTool()
  • 方法详细资料

    • initializeImportSupport

      protected void initializeImportSupport(ValueParser config)
      ImportSupport initialization
      参数:
      config - configuration values
    • configure

      protected void configure(ValueParser values)
      Looks for the "file" parameter and automatically uses initJSON(String) to parse the file (searched in filesystem current path and classpath) and set the resulting JSON object as the root node for this instance.
      覆盖:
      configure 在类中 ImportSupport
      参数:
      values - configuration values
    • initJSON

      protected void initJSON(String json)
      Initialize JSON content from a string.
      参数:
      json - JSON string
    • initJSON

      protected void initJSON(Reader reader)
      Initialize JSON content from a reader.
      参数:
      reader - JSON stream reader
    • parse

      public JsonTool parse(String json)
      Parses the given JSON string and uses the resulting Document as the root Node.
      参数:
      json - JSON string
      返回:
      new JsonTool
    • read

      public JsonTool read(String resource)
      Reads and parses a local JSON resource file
      参数:
      resource - resource name
      返回:
      new JsonTool
    • fetch

      public JsonTool fetch(String url)
      Reads and parses a remote or local URL
      参数:
      url - resource URL
      返回:
      new JSonTool
    • root

      public Object root()
      Get JSON root object.
      返回:
      root object or array
    • get

      public Object get(int index)
      Get nth element from root json array.
      参数:
      index - n
      返回:
      nth element, or null if root object is null or not an array
    • get

      public Object get(String key)
      Get a property from root object
      参数:
      key - property key
      返回:
      property value, or null
    • keys

      public Iterator<String> keys()
      Iterate keys of root object.
      返回:
      iterator
    • keySet

      public Set<String> keySet()
      Get set of root object keys.
      返回:
      keys set
    • iterator

      public Iterator iterator()
      Get an iterator. For a root object, returns an iterator over key names. For a root array, returns an iterator over contained objects.
      指定者:
      iterator 在接口中 Iterable
      返回:
      iterator
    • size

      public int size()
      Get size of root object or array.
      返回:
      size
    • toString

      public String toString()
      Convert JSON object or array into string
      覆盖:
      toString 在类中 Object
      返回:
      JSON representation of the root object or array