Convert JSON to Table and Query Specific Value
34 ビュー (過去 30 日間)
古いコメントを表示
I have the following JSON array:
{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}
How can I convert it to Table? Also, how can I query a specific row of the table? (Example: Find value of "Conf" where Name is "C.)
So far I have:
jsonData = '{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}'
structData = jsondecode(jsonData);
0 件のコメント
採用された回答
Florian Bidaud
2023 年 8 月 15 日
編集済み: Florian Bidaud
2023 年 8 月 15 日
jsonData = '{"views":[{"Name":"A","Conf":"High","View":"Negative"}, {"Name":"B","Conf":"Low","View":"Negative"}, {"Name":"C","Conf":"Low","View":"Negative"}]}'
structData = jsondecode(jsonData)
structDataTable = struct2table(structData.views)
structDataTable.Name
structDataTable.Conf{[structDataTable.Name{:}]=='C'}
8 件のコメント
Florian Bidaud
2023 年 8 月 16 日
編集済み: Florian Bidaud
2023 年 8 月 16 日
when you affect your value, first check if it's empty or not:
if ~isempty(structDataTable.Conf{strcmp([structDataTable.Name(:)],'Cat')})
value = structDataTable.Conf{strcmp([structDataTable.Name(:)],'Cat')};
else
value = nan;
end
その他の回答 (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!