Decode JSON into only struct array
이전 댓글 표시
Hi,
I am using jsondecode function in matlab to decode nested json script. I want the final output in struct array only. When some child keys are missing, I get a cell array. However, I would like to have all the keys in struct array and if it the child key is not present, I want that key with empty value. Is this function foreseen to have this functionality? is there any fast alternaive to achieve it?
Thanks
댓글 수: 5
TADA
2019년 7월 2일
Do you know in advance what the format of your struct is?
Your requirements are a bit unclear. The JSON that generates a structure would be completely different from the one that generates a cell array.
>>jsondecode('{"a":"b", "c":"d"}') %generate a structure with two fields
ans =
struct with fields:
a: 'b'
c: 'd'
>>jsondecode('{"a":["b", "e"], "c":["d", "f"]}') %the [] in the json generates a cell array
ans =
struct with fields:
a: {2×1 cell}
c: {2×1 cell}
The two JSON strings are completely different and it wouldn't make much sense to generate a structure from ["b", "e"]
Can you clarify what you want, possibly with some example JSON.
Parth-Raj Singh
2019년 7월 2일
편집: Parth-Raj Singh
2019년 7월 2일
Rik
2019년 7월 2일
Shouldn't the JSON string not be like the one below? Then the string itself would encode an empty element.
jsondecode('{"a":[{"b":2,"c":[]},{"b":3,"c":4}]}')
Alternatively, you could write a parser that ensures every field has the same subfields.
Parth-Raj Singh
2019년 7월 2일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 JSON Format에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!