How to pass single element JSON arrays using webwrite

4 Ansichten (letzte 30 Tage)
Daniel
Daniel am 20 Mai 2015
Kommentiert: Guillaume am 20 Mai 2015
Hi,
The question in brief:
"Can anyone advise how to pass single element JSON arrays using webwrite, and how to have them interpreted as integers rather than doubles?"
The problem:
I'm trying to pass a JSON value to an external API, over which I have no control.
The API wants to see:
{
"method": "blahblah",
"params": [1],
"id": 1,
"version": "1.0"
}
Which is confirmed to work. I know a single value array is maybe a bit silly. But it is what is required. And the API rejects a single number value without the square brackets.
In order to pass that to the external API, I am using webwrite
method = 'blahblah';
params = [1];
id = 1;
version = '1.0';
data = struct('method',method,'params',params, 'id',id,'version',version);
options = weboptions('MediaType','application/json');
response = webwrite('URL',data,options)
This generates the following JSON output:
{"method":"blahblah","params":1,"id":1,"version":"1.0"}
As you can see, everything is great except for the single element array value for "params", which is just a number instead of an array.
i.e
"params":1 % which I don't want (doesn't work)
instead of
"params": [1] % which I do want (works)
Drilling down, the matlab-json converter used by webwrite is
mls.internal.toJSON
We can see the function writes JSON arrays when it is passed arrays (although it converts them to doubles, which my API doesn't like at all):
>> mls.internal.toJSON(struct('params',[2,3]))
ans =
{"params":[2.0000000000000000,3.0000000000000000]}
But when passed a single value, it reverts to passing a simple JSON number, rather than a single element array:
>> mls.internal.toJSON(struct('params',[2]))
ans =
{"params":2}
Can anyone advise how to pass single element JSON arrays using webwrite, and how to have them interpreted as integers rather than doubles?
Any assistance hugely appreciated.

Akzeptierte Antwort

Guillaume
Guillaume am 20 Mai 2015
It looks like using a cell array instead of a matrix solves both of your problems (no '.0000' and always an array):
>>mls.internal.toJSON(struct('method', 'blahblah', 'params', {{2 3}})) %note the double {{ to prevent the cell being interpreted as an array of struct
ans = {"method":"blahblah","params":[2,3]}
>>mls.internal.toJSON(struct('method', 'blahblah', 'params', {{2}}))
ans = {"method":"blahblah","params":[2]}
So, basically use:
data = struct('method',method,'params', {num2cell(params)}, 'id',id,'version',version);
As usual, Mathworks have failed to properly document their API.
  2 Kommentare
Daniel
Daniel am 20 Mai 2015
Thanks, that works perfectly.
I'd played around with combinations of {} [] and '', but not thought to try the double curly-brace.
It's quite a new function (I had to update from 2014b to 2015a to get the JSON capabilities for webwrite), so I thought I'd put a bit more effort into describing the problem and tagging it properly, as I'm sure there will be a fair few others tripped up by this.
I'm sure your answer will prove useful to others.
Cheers
Guillaume
Guillaume am 20 Mai 2015
Note that the double curly brace is required because it's inside the struct function. (Otherwise struct maps the cell array into an array of structures with scalar field.
You would not need the double brace, if using field notation:
s1 = struct('a', {2 3 4}) %create structure s1(1).a = 2, s1(2).a = 3 s1(3).a = 4
s2 = struct('a', {{2 3 4}}) %create structure s2.a = {2 3 4}
s3.a = {2 3 4} %just assign cell to field.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by