Vector dimesions are different before encoding json and after decoding json
11 次查看(过去 30 天)
显示 更早的评论
Before encoding to json file the mat files are having vectors are as row vector. But after encoding (jsonencode) and decoding(jsondecode), the vectors are changed to column vectors in workspace. And I want the same dimensions after decoding also, how can I get that ?
b.c = 22;
a= jsonencode(b);
d=jsondecode(a);
采纳的回答
Ive J
2022-3-3
编辑:Ive J
2022-3-3
How about this?
mystr.f1 = 1:4;
mystr.f2 = (1:4).';
js = jsonencode(mystr);
newstr = jsondecode(js)
fnames = fieldnames(mystr);
fnames = fnames(structfun(@isrow, mystr)); % field names to be changed
for i = 1:numel(fnames)
newstr.(fnames{i}) = newstr.(fnames{i}).';
end
newstr
3 个评论
Ive J
2022-3-3
jsondecode output will be always a column vector irrespective of your input structure. So, you have to find another way of passing dimensions of vectors stored in your initial structure (mystr in my example), because you cannot infer it from a decoded json anyway. You can also work with mps.json.encode which is the schema for MATLAB Production Server, and especially designed for MATLAB data types. Note the difference between column and row vectors in this case:
x.a = 1:3; x.b = (1:3)';
js = mps.json.encode(x)
% decode again
newx = mps.json.decode(js)
更多回答(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!