How can I read a .json file?

1,673 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(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,2:04
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

Community Treasure Hunt

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

Start Hunting!

Translated by