主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

mps.json.encoderequest

使用 MATLABMATLAB Production Server JSON 架构将服务器请求中的数据转换为 JSON 文本

说明

text = mps.json.encoderequest(rhs) 使用 MATLAB®MATLAB Production Server™ 的 JSON 架构对输入到已部署的函数的请求进行编码。它构建一个服务器请求,其中包括调用 MATLAB Production Server 所需的 MATLAB 变量和选项(例如 'Nargout''OutputFormat'

示例

text = mps.json.encoderequest(rhs,Name,Value) 可针对特定输入用例,使用一个或多个名称-值对组参量来指定额外选项。

示例

示例

全部折叠

mps.json.encoderequest({[1 2 3 4]})
ans =
    '{"rhs":[[[1,2,3,4]]],"nargout":1,"outputFormat":{"mode":"small","nanType":"string"}}'
rhs = {['Red'], [15], [1 3; 5 7], ['Green']};
mps.json.encoderequest(rhs, 'Nargout', 3, 'OutputFormat', 'large')
ans =
    '{"rhs":["Red",15,[[1,3],[5,7]],"Green"],"nargout":3,"outputFormat":{"mode":"large","nanType":"string"}}'

使用 MATLAB 函数 horzcat 水平连接两个矩阵。

a = [1 2; 5 6];
b = [3 4; 7 8];
mps.json.encoderequest({horzcat(a,b)})
ans =
    '{"rhs":[[[1,2,3,4],[5,6,7,8]]],"nargout":1,"outputFormat":{"mode":"small","nanType":"string"}}'

执行 mps.json.encoderequestmps.json.decoderesponse,以使用 webwrite 调用在 MATLAB Production Server 上部署的函数。在本例中,学生姓名及他们对应的分数被部署到 MATLAB Production Serversortstudents 函数中,该函数根据学生的分数对他们进行排序。返回的结果相当于从 MATLAB 调用函数 sortstudents(struct('name', 'Ed', 'score', 83), struct('name', 'Toni', 'score', 91)) 得到的数据。

假设有一个可部署存档 studentapp,该存档中包含部署到服务器的 MATLAB 函数 sortstudents

注意

确保使用不同的流程(例如 cURL 或 Postman)调用测试服务器。如果您使用 webread 对 MATLAB 执行此操作,请使用与启动测试客户端的 MATLAB 进程不同的进程。

data = {struct('name', 'Ed', 'score', 83), struct('name', 'Toni', 'score', 91)};
body = mps.json.encoderequest(data);

options = weboptions;

% Create a weboptions object that instructs webread to return JSON text
options.ContentType = 'text';

% Create a weboptions object that instructs webwrite to encode character vector data as JSON to post it to a web service
options.MediaType = 'application/json';    

response = webwrite('http://localhost:9910/studentapp/sortstudents', body, options);

result = mps.json.decoderesponse(response);

输入参数

全部折叠

部署在 MATLABMATLAB Production Server 上的函数的输入参量,被调用,指定为单元向量。

名称-值参数

全部折叠

Name1=Value1,...,NameN=ValueN 的形式指定可选参量对组,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

如果使用的是 R2021a 之前的版本,请使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: mps.json.encoderequest(rhs, 'Format', 'large')

部署在 MATLABProduction Server 上的函数的输出参量的数量,指定为由 ' Nargout ' 和输出参量的数量组成的逗号分隔的对。

mps.json.encoderequest(rhs, 'Nargout', 3).

用于编码 rhs 格式,指定为以逗号分隔的对,由 ' Format ' 和格式 ' small ' 或 ' large ' 组成。

small 格式是采用 JSON 格式的 MATLAB 数据类型的简化表示,而 large 格式则是更通用的表示。有关详细信息,请参阅MATLAB 数据类型的 JSON 表示 (MATLAB Production Server)

NaN 中对 Inf-Infrhs 进行编码的格式,指定为由 ' NaNInfType ' 和 JSON 数据类型 ' string ' 和 ' object ' 组成的逗号分隔对。

部署在 MATLABMATLABProduction Server 上的函数的响应格式,指定为由 ' OutputFormat ' 和格式 ' small ' 或 ' large ' 组成的逗号分隔对。

使用 mps.json.encoderequest(rhs, 'OutputFormat', 'large') 设置输出格式。

部署在 MATLABMATLABProduction Server 上的函数的响应类型包含 NaNInf-Inf,指定为以逗号分隔的对,由“ OutputNaNInfType ”和 JSON 数据类型“ string ”和“ object ”组成。

使用 mps.json.encoderequest(rhs, 'OutputNaNInfType', 'object') 设置输出响应的 NaN 类型。

该选项表示格式化 text 以提高可读性,指定为以逗号分隔的对组,其中包含 'PrettyPrint' 和逻辑 'true' 或 'false'。语法为 mps.json.encoderequest(rhs,'PrettyPrint',true)

输出参量

全部折叠

遵从 MATLAB Production Server JSON 模式的 JSON 格式文本,以字符向量形式返回。

版本历史记录

在 R2018a 中推出