5.4.3 JSONデータのエンコード/デコード
JavaScript Object Notation (略して JSON) は、人間が判読可能な構造化されたデータ形式として非常に一般的です。GNU Octave は、次の 2 つの関数を使用してこの形式のエンコードとデコードをサポートしています。
: JSON_txt = jsonencode (object)
: JSON_txt = jsonencode (…, "ConvertInfAndNaN", TF)
: JSON_txt = jsonencode (…, "PrettyPrint", TF)
Octave データ型を JSON テキストにエンコードします。
入力オブジェクトはエンコードする Octave 変数です。
出力JSON_txtは、オブジェクトをエンコードした結果を含む JSON テキストです 。
オプションの値"ConvertInfAndNaN"が true の場合NaN、、、、 の値NAは出力でに変換されます 。false の場合、元の値のままになります。このオプションのデフォルト値は true です。 -InfInf"null"
オプションの値"PrettyPrint"が true の場合、出力テキストにはインデントと改行が含まれます。false の場合、出力は圧縮され、空白なしで書き込まれます。このオプションのデフォルト値は false です。
プログラミングノート:
複素数はサポートされていません。
classdef オブジェクトは最初に構造体に変換され、次にエンコードされます。
エスケープ文字 (例: "\n") を保持するには、一重引用符で囲まれた文字列を使用します。
"\0"二重引用符で囲まれた文字列内の ヌル文字 ( ) の後のすべての文字は、エンコード中に削除されます。
配列のエンコードとデコードでは、配列の次元が保持されるとは限りません。特に、行ベクトルは列ベクトルに再形成されます。
JSON は Octave よりも少ないデータ型をサポートするため、エンコードとデコードでは Octave データ型が保持されることは保証されません。たとえば、 をエンコードしてint8からデコードすると、 が得られますdouble。
この表は、Octave データ型から JSON データ型への変換を示しています。
Octave data type JSON data type logical scalar Boolean logical vector Array of Boolean, reshaped to row vector logical array nested Array of Boolean numeric scalar Number numeric vector Array of Number, reshaped to row vector numeric array nested Array of Number NaN, NA, Inf, -Inf when "ConvertInfAndNaN" = true "null" NaN, NA, Inf, -Inf when "ConvertInfAndNaN" = false "NaN", "NaN", "Infinity", "-Infinity" empty array "[]" character vector String character array Array of String empty character array "" cell scalar Array cell vector Array, reshaped to row vector cell array Array, flattened to row vector struct scalar Object struct vector Array of Object, reshaped to row vector struct array nested Array of Object classdef object Object
例:
jsonencode ([1, NaN; 3, 4])
⇒ [[1,null],[3,4]]
jsonencode ([1, NaN; 3, 4], "ConvertInfAndNaN", false)
⇒ [[1,NaN],[3,4]]
## Escape characters inside a single-quoted string
jsonencode ('\0\a\b\t\n\v\f\r')
⇒ "\\0\\a\\b\\t\\n\\v\\f\\r"
## Escape characters inside a double-quoted string
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}
See also: jsondecode.
: object = jsondecode (JSON_txt)
: object = jsondecode (…, "ReplacementStyle", rs)
: object = jsondecode (…, "Prefix", pfx)
: object = jsondecode (…, "makeValidName", TF)
JSON 形式のテキストをデコードします。
入力JSON_txtは、JSON テキストを含む文字列です。
出力オブジェクトは、 JSON_txtをデコードした結果を含む Octave オブジェクトです。
"ReplacementStyle"オプションと の詳細については"Prefix"、を参照してくださいmatlab.lang.makeValidName。
オプションの値"makeValidName"が false の場合、名前は変更されずmatlab.lang.makeValidName、 "ReplacementStyle"および"Prefix"オプションは無視されます。
注意: JSON テキストのデコードとエンコードでは、一部の名前が によって変更される可能性があるため、元のテキストが再現される保証はありませんmatlab.lang.makeValidName。
この表は、JSON データ型から Octave データ型への変換を示しています。
JSON data type Octave data type Boolean scalar logical Number scalar double String vector of characters Object scalar struct (field names of the struct may be different from the keys of the JSON object due to matlab_lang_makeValidName null, inside a numeric array NaN null, inside a non-numeric array empty double array [] Array, of different data types cell array Array, of Booleans logical array Array, of Numbers double array Array, of Strings cell array of character vectors (cellstr) Array of Objects, same field names struct array Array of Objects, different field names cell array of scalar structs
Examples:
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
See also: jsonencode, matlab.lang.makeValidName.