5.4.3 JSON 数据编码/解码

JavaScript Object Notation(简称 JSON)是一种非常常见的、人类可读的结构化数据格式。GNU Octave 支持通过以下两个函数对该格式进行编码和解码。

 
JSON_txt = jsonencode (object)
JSON_txt = jsonencode (…, "ConvertInfAndNaN", TF)
JSON_txt = jsonencode (…, "PrettyPrint", TF)

将 Octave 数据类型编码为 JSON 文本。

输入 object 是要编码的 Octave 变量。

输出 JSON_txt 是包含 object 编码结果的 JSON 文本。

若选项 "ConvertInfAndNaN" 的值为 true,则 NaNNA-InfInf 值将在输出中转换为 "null"。若为 false,则它们将保留原始值。此选项的默认值为 true。

若选项 "PrettyPrint" 的值为 true,则输出文本将包含缩进和换行符。若为 false,则输出将被压缩,不包含空白字符。此选项的默认值为 false。

编程说明:

  • 不支持复数。
  • classdef 对象首先转换为结构体,然后再进行编码。
  • 要保留转义字符(例如 "\n"),请使用单引号字符串。
  • 双引号字符串中,空字符("\0")之后的每个字符将在编码过程中被丢弃。
  • 对数组进行编码和解码不能保证保留数组的维度。具体来说,行向量将被重塑为列向量。
  • 编码和解码不能保证保留 Octave 数据类型,因为 JSON 支持的数据类型比 Octave 少。例如,若对 int8 进行编码再解码,将得到 double 类型。

下表展示了 Octave 数据类型到 JSON 数据类型的转换:

Octave 数据类型JSON 数据类型
logical scalarBoolean
logical vectorArray of Boolean, reshaped to row vector
logical arraynested Array of Boolean
numeric scalarNumber
numeric vectorArray of Number, reshaped to row vector
numeric arraynested Array of Number
NaN, NA, Inf, -Inf
"ConvertInfAndNaN" = true
"null"
NaN, NA, Inf, -Inf
"ConvertInfAndNaN" = false
"NaN", "NaN", "Infinity", "-Infinity"
empty array"[]"
character vectorString
character arrayArray of String
empty character array""
cell scalarArray
cell vectorArray, reshaped to row vector
cell arrayArray, flattened to row vector
struct scalarObject
struct vectorArray of Object, reshaped to row vector
struct arraynested Array of Object
classdef objectObject

示例:

jsonencode ([1, NaN; 3, 4])
⇒  [[1,null],[3,4]]

jsonencode ([1, NaN; 3, 4], "ConvertInfAndNaN", false)
⇒  [[1,NaN],[3,4]]

## 单引号字符串中的转义字符
jsonencode ('\0\a\b\t\n\v\f\r')
⇒  "\\0\\a\\b\\t\\n\\v\\f\\r"

## 双引号字符串中的转义字符
jsonencode ("\a\b\t\n\v\f\r")
⇒  "\u0007\b\t\n\u000B\f\r"

jsonencode ([true; false], "PrettyPrint", true)
⇒  ans = [
     true,
     false
   ]

jsonencode (['foo', 'bar'; 'foo', 'bar'])
⇒  ["foobar","foobar"]

jsonencode (struct ('a', Inf, 'b', [], 'c', struct ()))
⇒  {"a":null,"b":[],"c":{}}

jsonencode (struct ('structarray', struct ('a', {1; 3}, 'b', {2; 4})))
⇒  {"structarray":[{"a":1,"b":2},{"a":3,"b":4}]}

jsonencode ({'foo'; 'bar'; {'foo'; 'bar'}})
⇒  ["foo","bar",["foo","bar"]]

jsonencode (containers.Map({'foo'; 'bar'; 'baz'}, [1, 2, 3]))
⇒  {"bar":2,"baz":3,"foo":1}

另请参阅: jsondecode

 
object = jsondecode (JSON_txt)
object = jsondecode (…, "ReplacementStyle", style)
object = jsondecode (…, "Prefix", pfx)
object = jsondecode (…, "makeValidName", TF)

解码 JSON 格式的文本。

输入 JSON_txt 是包含 JSON 文本的字符串。

输出 object 是 Octave 对象,包含对 JSON_txt 进行解码的结果。

有关选项 "ReplacementStyle""Prefix" 的更多信息,请参见 matlab.lang.makeValidName

若选项 "makeValidName" 的值为 false,则名称将不会被 matlab.lang.makeValidName 改变,且 "ReplacementStyle""Prefix" 选项将被忽略。

注意:对 JSON 文本进行解码和编码不能保证还原出原始文本,因为某些名称可能会被 matlab.lang.makeValidName 改变。

下表展示了 JSON 数据类型到 Octave 数据类型的转换:

JSON 数据类型Octave 数据类型
Booleanscalar logical
Numberscalar double
Stringvector of characters
Objectscalar struct(由于 matlab_lang_makeValidName 的处理,结构体的字段名称可能与 JSON 对象的键不同)
null, inside a numeric arrayNaN
null, inside a non-numeric arrayempty double array []
Array, of different data typescell array
Array, of Booleanslogical array
Array, of Numbersdouble array
Array, of Stringscell array of character vectors (cellstr)
Array of Objects, same field namesstruct array
Array of Objects, different field namescell array of scalar structs

示例:

jsondecode ('[1, 2, null, 3]')
    ⇒  ans =

      1
      2
    NaN
      3

jsondecode ('["foo", "bar", ["foo", "bar"]]')
    ⇒  ans =
       {
         [1,1] = foo
         [2,1] = bar
         [3,1] =
         {
           [1,1] = foo
           [2,1] = bar
         }

       }

jsondecode ('{"nu#m#ber": 7, "s#tr#ing": "hi"}', ...
            'ReplacementStyle', 'delete')
    ⇒  scalar structure containing the fields:

         number = 7
         string = hi

jsondecode ('{"nu#m#ber": 7, "s#tr#ing": "hi"}', ...
            'makeValidName', false)
    ⇒  scalar structure containing the fields:

         nu#m#ber = 7
         s#tr#ing = hi

jsondecode ('{"1": "one", "2": "two"}', 'Prefix', 'm_')
    ⇒  scalar structure containing the fields:

         m_1 = one
         m_2 = two

另请参阅: jsonencode, matlab.lang.makeValidName


版权所有 © 2024-2026 Octave中文网

ICP备案/许可证号:黑ICP备2024030411号-4