无法在SAS中读取JSON输出

时间:2014-10-10 17:31:53

标签: json sas

我正在尝试解析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,"

2 个答案:

答案 0 :(得分:0)

如果您不熟悉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;

答案 1 :(得分:0)

SAS 9.4具有用于处理Json的Proc DS2解决方案

相关问题