How to pass single element JSON arrays using webwrite
4 次查看(过去 30 天)
显示 更早的评论
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}
It looks like similar problems have existed elsewhere: (Java) http://stackoverflow.com/questions/17003823/make-jackson-interpret-single-json-object-as-array-with-one-element
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.
0 个评论
采纳的回答
Guillaume
2015-5-20
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 个评论
Guillaume
2015-5-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!