gpt4 book ai didi

json - 无法读取 SAS 中的 JSON 输出

转载 作者:行者123 更新时间:2023-12-02 04:52:47 30 4
gpt4 key购买 nike

我正在尝试从 SAS 中的 JSON 文件中解析出数据,但卡在了我的代码中。我无法发布所有 JSON 内容,因为它太长了,但我可以发布部分相关内容。下面是我的 SAS 代码;当我运行它时,我在日志中看到“LOST CARD”的内容,我不确定那是什么意思。

部分代码被注释掉了,因为代码实际上可以工作,直到我进一步了解我的变量列表......我想知道......这个错误(“LOST CARD”)是否与“lrecl”有关行和某些变量的长度?

提前感谢您可能提供的任何帮助!

SAS 代码:

filename otp1 '...\output\otp1.json';

data work.otp1sas;
infile otp1 lrecl = 32000 /*truncover*/ scanover;
input

@'"startTime":' startTime $20.
@'"endTime":' endTime $20.
@'"walkTime":' walkTime $20.
@'"transitTime":' transitTime $20.
/*@'"waitingTime":' waitingTime $20.
@'"walkDistance":' walkDistance $20.
@'"walkLimitExceeded":' walkLimitExceeded $20.
@'"transfers":' transfers $20.*/;

startTime = scan(startTime,1,',"');
endTime = scan(endTime,1,',"');
walkTime = scan(walkTime,1,',"');
transitTime = scan(transitTime,1,',"');
/*waitingTime = scan(waitingTime,1,',"');
walkDistance = scan(walkDistance,1,',"');
walkLimitExceeded = scan(walkLimitExceeded,1,',"');
transfers = scan(transfers,1,',"');*/

run;

JSON 内容(与上述代码相关):

{"requestParameters":{"date":"03-14-2014","mode":"TRANSIT,WALK","arriveBy":"false","showIntermediateStops":"false","fromPlace":"33.8134605,-84.34973148","itinIndex":"0","toPlace":"33.80882004,-84.39769799","time":"10:00am","maxWalkDistance":"3218.688"},"plan":{"date":1394805600000,"from":{"name":"Arlington Avenue Northeast","stopId":null,"stopCode":null,"platformCode":null,"lon":-84.34880165944314,"lat":33.81255118156955,"arrival":null,"departure":null,"orig":null,"zoneId":null,"stopIndex":null},"to":{"name":"Collier Road Northwest","stopId":null,"stopCode":null,"platformCode":null,"lon":-84.39787235641106,"lat":33.808417712941896,"arrival":null,"departure":null,"orig":null,"zoneId":null,"stopIndex":null},"itineraries":[{"duration":2630.0,"startTime":1394806613000,"endTime":1394809243000,"walkTime":994,"transitTime":1273,"waitingTime":363,"walkDistance":1280.0733818655874,"walkLimitExceeded":false,"elevationLost":0.0,"elevationGained":0.0,"transfers":2,"

最佳答案

如果您没有足够的能力拥有 SAS 9.3+,您可以使用以下内容

filename otp1 '...\output\otp1.json';

data work.otp1sas;
format
equation $60.
variable $32.
value $32.

startTime $20.
endTime $20.
walkTime $20.
transitTime $20.
waitingTime $20.
walkDistance $20.
walkLimitExceeded $20.
transfers $20.;

** read the JSON and isolate the relevant object **;
infile otp1 lrecl = 32000 truncover ;
input @'"itineraries":[' jsonArray $32000.;
jsonArray = scan(jsonArray, 1, ']');

** separate the objects **;
do objNr = 1 to countw(jsonArray, '}{');
jsonObject = strip(scan(jsonArray, objNr, '}{'));

** separate the equations **;
do varNr = 1 to countw(jsonObject, ',');
equation = strip(scan(jsonObject, varNr, ','));
variable = scan(equation, 1, '"');
value = substr(equation, index(equation, ':') + 1);

** handle cases where a string value contains a comma **;
do while (varNr LT countw(jsonObject,',')
and substr(value, 1, 1) EQ '"'
and substr(value, length(value), 1) NE '"' );
varNr = varNr + 1;
value = trim(value) ||','|| strip(scan(jsonObject, varNr, ','));
end;

select (variable);
when ('startTime') startTime = dequote(value);
when ('endTime') endTime = dequote(value);
when ('walkTime') walkTime = dequote(value);
when ('transitTime') transitTime = dequote(value);
when ('waitingTime') waitingTime = dequote(value);
when ('walkDistance') walkDistance = dequote(value);
when ('walkLimitExceeded') walkLimitExceeded = dequote(value);
when ('transfer') transfer = dequote(value);
otherwise put varNr= variable= value=;
end;
end;
if countw(jsonObject, ',') GT 1 then output;
end;
run;

关于json - 无法读取 SAS 中的 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26304964/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com