VDS Renaissance 的界面提供多种语言。官方文档以英语维护,以确保提供统一、一致且始终最新的参考。

JSON Documents

[VDS7] DialogScript has special support for JSON documents, in the same style as its support for XML documents. JSON is the data format spoken by most web APIs and by AI models, so it is the natural way to handle their responses.

A JSON document holds information with a hierarchical (tree) structure, built from objects (sets of named values), arrays (ordered lists of values), and scalar values (strings, numbers, true/false, null). For example:

{
  "users": [
    { "name": "John Smith", "age": 43 },
    { "name": "Jim Black",  "age": 29 }
  ]
}

Handles

A JSON document is held in memory and accessed through a numeric handle, exactly like an XML document. You create one with the @NEW(JSON) function, which returns the handle, and you release it with JSON CLOSE:

%h = @new(json)
REM ... work with the document ...
json close,%h

The document is mutated with the JSON command and read with the @JSON function.

Paths

Items are addressed by a dotted path from the root of the document — the notation that JSON APIs and AI models naturally emit:

Path Refers to
users the array named users
users.0 the first element of that array
users.0.name the name value of the first element

An object key is given by its name; an array index is given by an integer. The form users[0] is accepted as an alias for users.0. An empty path refers to the whole document (the root).

Iterating

Iteration uses ordinary DialogScript control flow — there is no new grammar. Use @JSON(count,...) to get the number of elements, then index by position:

%h = @new(json)
json loadfile,%h,response.json
%n = @json(count,%h,users)
%i = 0
while @less(%i,%n)
  write console,@json(value,%h,users.%i.name)@lf()
  %i = @succ(%i)
wend
json close,%h

JSON typed directly in a script

JSON normally arrives from a source — an AI or HTTP response, a file, or a function — and in that case it travels through a variable or a file without alteration (json parse,%h,%response or json loadfile). JSON typed literally into a script line (for example %j = {"a":1,"b":2}) is subject to the usual DialogScript rules (comma is a parameter separator, quotes are removed) and would be truncated. To build a document from scratch, use json set / json add rather than a literal.

See also