Acquiring a JSON from a webpage
10 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'd need to find a way to acquire a JSON file from a web portal. I can do it manually just by going to the site and save in JSON (see screenshot). However, I'd like to be able to do it directly from Matlab, but webread just returns the HTML code, not the data I'm looking for. I tried using the options:
options = weboptions('ContentType','json');
But it failed. Any idea is welcome
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/698997/image.jpeg)
Thanks,
Tero
0 个评论
回答(1 个)
Yongjian Feng
2021-7-30
Try this:
headers = {'Content-Type' 'application/json'; 'Accept' 'application/json'};
options = weboptions('HeaderFields',headers);
webread('http://ip.jsontest.com', options)
2 个评论
Yongjian Feng
2021-8-3
I see. It seems like you just got back the html for the login page. In general, there shall be an API key for authentication. Or try to put your username/password into the headers as well.
另请参阅
类别
在 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!