How to send JSON data to a server

27 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2022 年 11 月 8 日
回答済み: MathWorks Support Team 2022 年 11 月 8 日

How can I send my JSON data to a server?

採用された回答

MathWorks Support Team
MathWorks Support Team 2022 年 11 月 8 日
The examples below show how you can send a JSON string or MATLAB data to a server with JSON encoding. This example will use the following server.
url = 'https://requestserver.mathworks.com';
If you already have a JSON string
Suppose you already have the following JSON string.
json = '[{"Name":"Jon","Job":"Doctor"},{"Name":"Sue","Job":"Engineer"}]'
The examples below show you how to send this JSON string to a server using webwrite or HTTP Interface.

Using webwrite

opts = weboptions();
opts.MediaType = 'application/json';
response = webwrite(url, json, opts);

Using HTTP Interface

import matlab.net.http.*
import matlab.net.http.field.*
mb = MessageBody;
mb.Payload = json;
request = RequestMessage('POST', ...
[ContentTypeField('application/json')], ...
mb)
response = request.send(url);
If you have MATLAB data which needs JSON encoding
Suppose you have the following MATLAB struct data.
employee(1).Name = 'Jon';
employee(1).Job = 'Doctor';
employee(2).Name = 'Sue';
employee(2).Job = 'Engineer';
The examples below show you how to send this MATLAB data to a server using webwrite or HTTP Interface with JSON encoding.

Using webwrite

opts = weboptions();
opts.MediaType = "application/json";
response = webwrite(url, employee, opts);

Using HTTP Interface

import matlab.net.http.*
import matlab.net.http.field.*
mb = MessageBody;
mb.Data = employee;
request = RequestMessage('POST', ...
[ContentTypeField('application/json')], ...
mb)
response = request.send(url);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by