Acquiring a JSON from a webpage

25 ビュー (過去 30 日間)
Tero
Tero 2021 年 7 月 30 日
コメント済み: Yongjian Feng 2021 年 8 月 3 日
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
Thanks,
Tero

回答 (1 件)

Yongjian Feng
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 件のコメント
Tero
Tero 2021 年 8 月 3 日
Thanks,
I tried this with my site, but it didn't work yet. Does it matter the data is behind a login screen? I can see it works fine with the example you gave
Yongjian Feng
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 ExchangeJSON Format についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by