Convert JSON to Table and Query Specific Value
45 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Monkey Coder
el 15 de Ag. de 2023
Comentada: Monkey Coder
el 16 de Ag. de 2023
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 comentarios
Respuesta aceptada
Florian Bidaud
el 15 de Ag. de 2023
Editada: Florian Bidaud
el 15 de Ag. de 2023
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 comentarios
Florian Bidaud
el 16 de Ag. de 2023
Editada: Florian Bidaud
el 16 de Ag. de 2023
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
Más respuestas (0)
Ver también
Categorías
Más información sobre JSON Format en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!