sas 解析json

时间:2022-01-08 07:35:31

代码:

proc ds2;
data _null_;
    method init();
        dcl package json j();
        dcl int rc tokenType parseFlags;
        dcl bigint lineNum colNum;
        dcl nvarchar(128) token abc t1;
        abc = 'xyz';
        t1 = '{"abc" : 1 }';
        rc = j.createParser( t1 );
             if (rc ne 0) then goto TestError;

* obj open;
        j.getNextToken( rc, token, tokenType, parseFlags, lineNum, colNum );
        if ( rc ne 0 ) then goto TestError;
        
        PUT "1、" rc= token= tokenType= parseFlags= lineNum= colNum= ;

* obj label;
        j.getNextToken( rc, token, tokenType, parseFlags, lineNum, colNum );
        if ( rc ne 0 ) then goto TestError;
        PUT "2、" rc= token= tokenType= parseFlags= lineNum= colNum= ;

* obj value;
        j.getNextToken( rc, token, tokenType, parseFlags, lineNum, colNum );
        if ( rc  ne 0) then goto TestError;
        
        PUT "3、" rc= token= tokenType= parseFlags= lineNum= colNum= ;

* obj close;
        j.getNextToken( rc, token, tokenType, parseFlags, lineNum, colNum );
        if ( rc ne 0 ) then goto TestError;
        
        
        PUT "4、" rc= token= tokenType= parseFlags= lineNum= colNum= ;

Exit:
        rc = j.destroyParser();
        return;

TestError:
        put 'Test ended abnormally.';
        goto Exit;

end;
enddata;
run;
quit;

运行日记:

. rc=0 token={ tokenType=64 parseFlags=0 lineNum=1 colNum=1
. rc=0 token=abc tokenType=256 parseFlags=1 lineNum=1 colNum=6
. rc=0 token=1 tokenType=512 parseFlags=3 lineNum=1 colNum=10
. rc=0 token=} tokenType=128 parseFlags=0 lineNum=1 colNum=12

GETNEXTTOKEN Method

Returns the next validated JSON language item or element from the JSON text.

Syntax

Form 1:

package.GETNEXTTOKEN (rc, token-type, parse-flags,);

Form 2:

package.GETNEXTTOKEN (rc, token, token-type, parse-flags,);

Form 3:

package.GETNEXTTOKEN (rc, token, token-type, parse-flags, line-number,
column-number);

Arguments

package

specifies an instance of the JSON package.

rc

specifies the variable to hold the return code value. Possible return code values are as follows:

0  Success
100  The output token argument's maximum length was not large enough and truncation occurred.

101  Done. Depending on the use case, this might or might not be expected.  
300  End of text. Depending on the use case, this might or might not be expected.  
301  An error occurred while parsing the text.

token-type

token-type can be one of the following values:

4  Boolean true
8  Boolean false
16  Left bracket ( [ )
32  Right bracket ( [ )
64  Left brace ( { )
128  Right brace ( } )
256  String
512  Numeric
1024  Null

parse-flags

parse-flags output value can be an integer flag set consisting of one or more of the following flags:

0x00000001  token is a label in an object
0x00000002  token is not complete
0x00000003  token is an integral numeric
0x00000004  token is a floating point number

token

is the next token or string.

line-number

Updates the given integer variable argument with the line number within the text where the token is located.

Tip You can use the line number to help determine the location of the token within the text.

column-number

Updates the given integer variable argument with the column number within the text where the token is located.

Tip You can use the column number to help determine the location of the token within the text.

Details

All of the arguments to the GETNEXTTOKEN method are passed by reference.

You can use the IS* methods to test the token type.