PROC GROOVY 解析JSON

 

 

参考:http://saslist.com/blog/category/json/

下载jdk和groovy
1.安装jdk 默认安装
2解压groovy:到C:\Program Files (x86)\
参照http://saslist.com/blog/category/json/ 关于groovy的那段,配置groovy,并设置系统变量:
**********
(1)JDK的配置:
设置环境变量%JAVA_HOME%\bin,值为放置解压文件的目录,如C:\Program Files (x86)\Java\jdk1.8.0_141
增加环境变量path,指向%GROOVY_HOME%\bin
(2)groovy配置
设置环境变量GROOVY_HOME,值为放置解压文件的目录,如C:\Program Files (x86)\groovy-2.4.12
增加环境变量path,指向%GROOVY_HOME%\bin
(3)找到SAS执行程序的路径,如C:\Program Files\SASHome\SASFoundation\9.4
该路径下的\nls\en的sasv9.cfg,查找JREOPTIONS,添加
-Dtkj.app.class.path=C:\Program Files (x86)\Groovy\groovy-2.4.12\embeddable\groovy-all-2.4.12.jar

3将解析结果放在csv里面,需要将opencsv-3.10放在groovy安装包下面embeddable下面

4.接下来就可以在Groovy控制台中编写脚本了,在Windows控制台(cmd)下输入groovyconsole命令进入groovy控制台,在里面输入代码 println "HelloWorld"回车,按组合键CTRL+R就可以看到运行结果了。

解析:

json:

{
"acct_nbr": 1234,
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
,
{
"acct_nbr": 3456,
"firstName": "Sam",
"lastName": "Jones",
"age": 32,
"address": {
"streetAddress": "25 2nd Street",
"city": "New Jersy",
"state": "NJ",
"postalCode": "10081"
}
}

解析代码:


%let groovydir=C:\Program Files (x86)\groovy-2.4.12\embeddable; *the location the groovy JARs are located at;

%let sourcefile=.\data\test_1.txt;
%let outfile=.\data\test_1.csv;

proc groovy classpath="&groovydir.\groovy-all-2.4.12.jar;&groovydir.\opencsv-3.10.jar";

submit "&sourcefile" "&outfile";
import groovy.json.*/*加载json包*/
import com.opencsv.*

def input = new File(args[0]).text
def output = new JsonSlurper().parseText(input)
def csvoutput = new FileWriter(args[1])

CSVWriter writer = new CSVWriter(csvoutput);

String[] header = new String[2];
header[0] = "user_basic";
header[1] = "user_blacklist";
writer.writeNext(header);

output.statuses.each {
String[] user_basic_content1 = new String[8];
content[0] = it.user_basic.user_age.toString();
content[1] = it.user_basic.user_city.toString();
content[2] = it.user_basic.user_gender.toString();
content[3] = it.user_basic.user_idcard.toString();
content[4] = it.user_basic.user_idcard_valid.toString();
content[5] = it.user_basic.user_name.toString();
content[6] = it.user_basic.user_phone.toString();
content[7] = it.user_basic.user_phone_city.toString();
writer.writeNext(user_basic_content1);
}

writer.close();

endsubmit;
quit;

posted @   Anni爱摩天轮  阅读(859)  评论(0编辑  收藏  举报
编辑推荐:
· [杂谈]如何选择:Session 还是 JWT?
· 硬盘空间消失之谜:Linux 服务器存储排查与优化全过程
· JavaScript是按顺序执行的吗?聊聊JavaScript中的变量提升
· [杂谈]后台日志该怎么打印
· Pascal 架构 GPU 在 vllm下的模型推理优化
阅读排行:
· 面试官:DNS解析都整不明白,敢说你懂网络?我:嘤嘤嘤!
· 2000 Star,是时候为我的开源项目更新下功能了
· [WPF UI] 为 AvalonDock 制作一套 Fluent UI 主题
· 不错的AI项目集合
· 基于.NET WinForm开发的一款硬件及协议通讯工具
点击右上角即可分享
微信分享提示