how can I cause jsondecode to add [ ]

9 visualizzazioni (ultimi 30 giorni)
uriel shitrit
uriel shitrit il 15 Dic 2021
Modificato: Prince Kumar il 20 Gen 2022
Hi
In order to create a JSON file that is suitable for another application out of Matlab, I arranged all the needed parameters as fieldName of struct array like in the code below. but ,unfortunately, in the output it missing [ ] on the "regions" subfieldNames .
how can I fix it with Matlab.
Many thanks to the helpers.
the code for 1 file as an example
for c=1:1 %numel(X)/2
jsonformat1.filename=string(J{c,1});
jsonformat1.size=J{c,2};
jsonformat1.regions.shape_attributes.name="polygon";
jsonformat1.regions.shape_attributes.all_points_x=J{c,3};
jsonformat1.regions.shape_attributes.all_points_y=J{c,4};
jsonformat1.regions.region_attributes.name="not difined";
jsonformat1.regions.region_attributes.type="unknown";
jsonformat1.regions.region_attributes.image_quality.good="true";
jsonformat1.regions.region_attributes.image_quality.frontal="true";
jsonformat1.regions.region_attributes.image_quality.good_illumination="true";
jsonformat1.file_attributes.caption={};
jsonformat1.file_attributes.public_domain="no";
jsonformat1.file_attributes.image_url={};
end
jsonencode(jsonformat1)
the output from matlab
'{"filename":"case100im16.jpg","size":207942,"region":{"shape_attributes":{"name":"polygon","all_points_x":[425,432,442,452,449,468,465,495],"all_points_y":[370,372,353,354,321,314,305,312,]},"region_attributes":{"name":"not difined","type":"unknown","image_quality":{"good":"true","frontal":"true","good_illumination":"true"}}},"file_attributes":{"caption":[],"public_domain":"no","image_url":[]}}'
the output needed to the other application ( addition of " [ " before "shape_attributes" and " ] "before "file_attributes")
'{"filename":"case100im16.jpg","size":207942,"region":[{"shape_attributes":{"name":"polygon","all_points_x":[425,432,442,452,449,468,465,495],"all_points_y":[370,372,353,354,321,314,305,312,]},"region_attributes":{"name":"not difined","type":"unknown","image_quality":{"good":"true","frontal":"true","good_illumination":"true"}}}],"file_attributes":{"caption":[],"public_domain":"no","image_url":[]}}'
  2 Commenti
Geoff Hayes
Geoff Hayes il 15 Dic 2021
@uriel shitrit - the [] indicates an array so it isn't clear to me why these square brackets would be required for the "region" field..unless you are allowing for multiple shape_attributes?
uriel shitrit
uriel shitrit il 15 Dic 2021
I really don't know why it was needed I just saw that when I manually add it to the file the other application succeeds to load and read the file

Accedi per commentare.

Risposte (1)

Prince Kumar
Prince Kumar il 19 Gen 2022
Modificato: Prince Kumar il 20 Gen 2022
Hi,
You can create another json which you want to put in [].
You can refer the following implementation
jsonformat1.filename= "file_name";
jsonformat1.size=12;
subjson.shape_attributes = "polygon";
subjson.region_attributes.name="not difined";
subjson.region_attributes.type="unknown";
jsonformat1.region = {subjson};
jsonformat1.file_attributes.public_domain="no";
jsonencode(jsonformat1)
In the above example subjson has the data that you want to put in [].
I hope you find this information useful.

Community Treasure Hunt

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

Start Hunting!

Translated by