필터 지우기
필터 지우기

How can I read a .json file?

조회 수: 1,483 (최근 30일)
Carlyle Hickman
Carlyle Hickman 2017년 2월 24일
편집: cui,xingxing 2024년 4월 27일
Hello I found some data on the web that requires me to open a .gz file and a .json. I was able to unzip the gz file in MATLAB but have not been able to read the .json.
My Code:
>> fname = 'meta_Electronics.json';
>> fid = fopen(fname);
>> raw = fread(fid,inf);
>> str = char(raw');
>> fclose(fid);
>> data = JSON.parse(str)
and this is the error that is displayed:
Undefined variable "JSON" or class "JSON.parse".
Can someone point me in the right direction, as far as what i need to change?
Cheers.
  댓글 수: 1
Adam
Adam 2017년 2월 24일
I looked on the File Exchange when I wanted to read a json file, then downloaded something from there and made various edits to it to speed it up a bit. There are a few submissions on there that will read json files.
If you haven't defined anything called JSON though then
data = JSON.parse(str);
is highly optimistic coding. It's a very good way to code from the top level down using idealised code, but only if you write the code that you are calling too rather than just assume it will appear somehow!

댓글을 달려면 로그인하십시오.

채택된 답변

Alka Nair
Alka Nair 2017년 11월 22일
fname = 'meta_Electronics.json';
fid = fopen(fname);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
val = jsondecode(str);
worked for me.
  댓글 수: 4
Tolga Karabiyikoglu
Tolga Karabiyikoglu 2020년 11월 12일
Cool- seems to work for me too
Juan Miguel Serrano Rodríguez
Juan Miguel Serrano Rodríguez 2021년 10월 19일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Elbi Mutluoglu
Elbi Mutluoglu 2020년 2월 13일
How can I read this json file as array.
{"flowSegmentData":{"frc":"FRC0","currentSpeed":30,"freeFlowSpeed":85,"currentTravelTime":28,"freeFlowTravelTime":10,"confidence":0.9900000095367432,"roadClosure":false,"coordinates":{"coordinate":[{"latitude":40.99520713231141,"longitude":29.11316083692566},{"latitude":40.9951128720138,"longitude":29.11348934584032},{"latitude":40.99499782625885,"longitude":29.113922597143414},{"latitude":40.99492447823734,"longitude":29.11425589372996},{"latitude":40.99481265425299,"longitude":29.114754874308034},{"latitude":40.9947156413892,"longitude":29.115239700057685},{"latitude":40.99469244741794,"longitude":29.115362205564793},{"latitude":40.99466308297875,"longitude":29.115527466260318},{"latitude":40.99465731538117,"longitude":29.11556668313179},{"latitude":40.99465324816397,"longitude":29.115588820406316},{"latitude":40.9946365659001,"longitude":29.115684510886638},{"latitude":40.994617484549394,"longitude":29.11581043877149},{"latitude":40.99460761712758,"longitude":29.115881261564937}]},"@version":"traffic-service 3.2.001"}}
  댓글 수: 1
Nitesh Panchal
Nitesh Panchal 2021년 4월 26일
편집: Nitesh Panchal 2021년 4월 26일
please use----- {jsondecode('[ ----------------please paste your json code here------------- ]')}
{jsondecode('[{"flowSegmentData":{"frc":"FRC0","currentSpeed":30,"freeFlowSpeed":85,"currentTravelTime":28,"freeFlowTravelTime":10,"confidence":0.9900000095367432,"roadClosure":false,"coordinates":{"coordinate":[{"latitude":40.99520713231141,"longitude":29.11316083692566},{"latitude":40.9951128720138,"longitude":29.11348934584032},{"latitude":40.99499782625885,"longitude":29.113922597143414},{"latitude":40.99492447823734,"longitude":29.11425589372996},{"latitude":40.99481265425299,"longitude":29.114754874308034},{"latitude":40.9947156413892,"longitude":29.115239700057685},{"latitude":40.99469244741794,"longitude":29.115362205564793},{"latitude":40.99466308297875,"longitude":29.115527466260318},{"latitude":40.99465731538117,"longitude":29.11556668313179},{"latitude":40.99465324816397,"longitude":29.115588820406316},{"latitude":40.9946365659001,"longitude":29.115684510886638},{"latitude":40.994617484549394,"longitude":29.11581043877149},{"latitude":40.99460761712758,"longitude":29.115881261564937}]},"@version":"traffic-service 3.2.001"}}]')}

댓글을 달려면 로그인하십시오.


cui,xingxing
cui,xingxing 2023년 8월 14일
편집: cui,xingxing 2024년 4월 27일
My personal opinion is that there is still no direct mapping from a generic readable json file to MATLAB's built-in types (struct or dictionary). The readstruct function is expected to support more file formats.
But on the other hand, you don't really need to call low-level functions such as fread, you can just use the fileread function to read the content and then decode it. For example, the following operation:
!unzip test.zip # see attachment file,use @Elbi Mutluoglu data for example
Archive: test.zip inflating: test.json
View json file contents, preview it.
!cat test.json
{ "flowSegmentData": { "frc": "FRC0", "currentSpeed": 30, "freeFlowSpeed": 85, "currentTravelTime": 28, "freeFlowTravelTime": 10, "confidence": 0.9900000095367432, "roadClosure": false, "coordinates": { "coordinate": [ { "latitude": 40.99520713231141, "longitude": 29.11316083692566 }, { "latitude": 40.9951128720138, "longitude": 29.11348934584032 }, { "latitude": 40.99499782625885, "longitude": 29.113922597143414 }, { "latitude": 40.99492447823734, "longitude": 29.11425589372996 }, { "latitude": 40.99481265425299, "longitude": 29.114754874308034 }, { "latitude": 40.9947156413892, "longitude": 29.115239700057685 }, { "latitude": 40.99469244741794, "longitude": 29.115362205564793 }, { "latitude": 40.99466308297875, "longitude": 29.115527466260318 }, { "latitude": 40.99465731538117, "longitude": 29.11556668313179 }, { "latitude": 40.99465324816397, "longitude": 29.115588820406316 }, { "latitude": 40.9946365659001, "longitude": 29.115684510886638 }, { "latitude": 40.994617484549394, "longitude": 29.11581043877149 }, { "latitude": 40.99460761712758, "longitude": 29.115881261564937 } ] }, "@version": "traffic-service 3.2.001" } }
myJsonFile = "test.json";
text = fileread(myJsonFile);
data = jsondecode(text)
data = struct with fields:
flowSegmentData: [1×1 struct]
% Access to the next level
fieldnames(data.flowSegmentData)
ans = 9×1 cell array
{'frc' } {'currentSpeed' } {'freeFlowSpeed' } {'currentTravelTime' } {'freeFlowTravelTime'} {'confidence' } {'roadClosure' } {'coordinates' } {'x_version' }
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com
  댓글 수: 1
cui,xingxing
cui,xingxing 2023년 8월 24일
Luckily, my expectations above (readstruct support for json reads, writestruct support for json saves) have been implemented by the new version, which has been supported since R2023b!

댓글을 달려면 로그인하십시오.


Hammad Ur Rahman
Hammad Ur Rahman 2024년 2월 9일
% Read JSON data from a file
jsonFileName = 'data.json';
jsonStr = fileread(jsonFileName);
% Convert JSON string to MATLAB variables
jsonData = jsondecode(jsonStr);
% Access JSON data using MATLAB variables
value = jsonData.key; % Replace 'key' with the actual key name

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by