Webread returns empty stucture, but urlread works,

Hi everyone, i have some issue about Webread
this is my code,
key = 'ZgFJoT6d3VEh1qz%2Fv%2Bj6SFAuefvQF%2BlboVR1njEK%2B8LcYTYddXSFE57fmAukUXPovujQLo9Fy05EvPpaGpH0ag%3D%3D';
baseurl_estate = 'http://apis.data.go.kr/1611000/nsdi/IndvdLandPriceService/attr/getIndvdLandPriceAttr?ServiceKey=';
start_year = '2020';
format = 'json';
num_of_row = '20';
for i = 1:height(stn_data)
url_estate = strcat(baseurl_estate,key,'&pnu=',stn_data.pnu(i,1:end),'&numOfRows=', num_of_row,'&format=',format,'&stdrYear=',start_year);
temp_estate = urlread(url_estate);
temp_estate = jsondecode(temp_estate);
for j = 1:19
if ~isempty(temp_estate.indvdLandPrices.field)
break;
else
url_estate = strcat(baseurl_estate,key,'&pnu=',stn_data.pnu(i,1:end-j),'&numOfRows=', num_of_row,'&format=',format,'&stdrYear=',start_year);
temp_estate = urlread(url_estate);
temp_estate = jsondecode(temp_estate);
end
end
if j == 19
fprintf("%d 에러발생",i)
cnt = cnt+1;
end
stn_estate(i,1) = temp_estate;
clear -regexp ^temp
i
end
In this code, urlread is works well, i mean it can return data
but, when i chagne my code use webread, webread doesn't return value.Webreads return an empty struct with no fields.
I want use webread cause when i use urlread and json, sometimes error was happened
'JSON syntax error at line 1, column 1 (character 1): expected value but found '<'.'
% function stn_data = make_estate(stn_data)
% get estate
% from 공공데이터 포털
% 지번마지막 자리로 결과가 안나오면 뒤에서부터 하나씩 제거
% if when this code delete 5th jibun that is error!!!
key = 'ZgFJoT6d3VEh1qz%2Fv%2Bj6SFAuefvQF%2BlboVR1njEK%2B8LcYTYddXSFE57fmAukUXPovujQLo9Fy05EvPpaGpH0ag%3D%3D';
baseurl_estate = 'http://apis.data.go.kr/1611000/nsdi/IndvdLandPriceService/attr/getIndvdLandPriceAttr?ServiceKey=';
start_year = '2020';
format = 'xml';
num_of_row = '20';
cnt = 0;
% conflict = '1';
% options = weboptions('Timeout', inf,'ContentType','json');
for i = 1:1
url_estate = strcat(baseurl_estate,key,'&pnu=',stn_data.pnu(i,1:end),'&numOfRows=', num_of_row,'&format=',format,'&stdrYear=',start_year);
temp_estate = webread(url_estate);
for j = 1:19
if ~isempty(temp_estate.indvdLandPrices.field)
break;
else
url_estate = strcat(baseurl_estate,key,'&pnu=',stn_data.pnu(i,1:end-j),'&numOfRows=', num_of_row,'&format=',format,'&stdrYear=',start_year);
temp_estate = urlread(url_estate);
temp_estate = jsondecode(temp_estate);
end
end
if j == 19
fprintf("%d 에러발생",i)
cnt = cnt+1;
end
stn_estate(i,1) = temp_estate;
clear -regexp ^temp
i
end
if ~(cnt == 0)
fprintf("%d 에러 횟수",cnt)
end
estate = arrayfun(@(x) x.indvdLandPrices.field(1).pblntfPclnd,stn_estate,'UniformOutput',false);
% stn_new = stn_data(1:100,:);
stn_data.estate = estate;

2 Comments

Can you share "stn_data" as "mat"?
i upload stn_data mat file

Sign in to comment.

 Accepted Answer

The script below uses webread instead of urlread. The point here is to use weboptions and set the ContentType to text.
load("save_before_estate.mat")
key = 'ZgFJoT6d3VEh1qz%2Fv%2Bj6SFAuefvQF%2BlboVR1njEK%2B8LcYTYddXSFE57fmAukUXPovujQLo9Fy05EvPpaGpH0ag%3D%3D';
baseurl_estate = 'http://apis.data.go.kr/1611000/nsdi/IndvdLandPriceService/attr/getIndvdLandPriceAttr?ServiceKey=';
start_year = '2020';
format = 'json';
num_of_row = '20';
for i = 1:height(stn_data)
url_estate = strcat(baseurl_estate,key, ...
'&pnu=',stn_data.pnu(i,1:end),'&numOfRows=', num_of_row,'&format=',format,'&stdrYear=',start_year);
% temp_estate = urlread(url_estate)
options = weboptions('ContentType','text'); % I found the API returns json in text format.
temp_estate = webread(url_estate, options)
temp_estate = jsondecode(temp_estate);
for j = 1:19
if ~isempty(temp_estate.indvdLandPrices.field)
break;
else
url_estate = strcat(baseurl_estate,key,'&pnu=',stn_data.pnu(i,1:end-j),'&numOfRows=', num_of_row,'&format=',format,'&stdrYear=',start_year);
temp_estate = urlread(url_estate);
temp_estate = jsondecode(temp_estate);
end
end
if j == 19
fprintf("%d 에러발생",i)
cnt = cnt+1;
end
stn_estate(i,1) = temp_estate;
clear -regexp ^temp
i
end

More Answers (0)

Products

Release

R2023a

Asked:

on 12 Oct 2023

Commented:

on 12 Oct 2023

Community Treasure Hunt

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

Start Hunting!