java解析从接口获取的json内容并写到excle(只写与标题匹配的值,并非把所有的接口返回值都写进去)
2024-09-26 21:33:16
需求:从接口中获取的一个json数组中有多个对象,每个对象中的值并非都需要,只需查出标题中的几项对应的值即可。且还需要按某个字段排序后依次写到excel
实现方法如下:
package jansonDemo; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.*; public class TestJsonToExcel {
public static void main(String[] args) {
XSSFWorkbook workbook;
XSSFSheet sheet;
XSSFRow row;
XSSFCell cell; //创建excel工作薄
workbook = new XSSFWorkbook();
//创建一个工作表sheet
sheet = workbook.createSheet("stationInfo");
//创建第一行
row = sheet.createRow(0);
//创建一个单元格
cell = null; //定义标题栏,放到数组中
String stationInfo_title[] =
{"Pictures","StationLng","SiteGuide","Address","ServiceTel",
"SupportOrder","OperatorID","StationID","Remark"}; //在第一行插入标题栏
for (int i=0;i<stationInfo_title.length;i++) {
cell = row.createCell(i);
cell.setCellValue(stationInfo_title[i]);
} //接着从第二行开始写入内容:
//根据标题栏中的字段筛选相应的值,并非是获取所有接口返回的值
CommonFunc cf = new CommonFunc();
try {
int rownum = 1;
String cellContent = "";
JSONObject decrptobj = cf.getStationInfo(); //获取从接口返回的json对象
JSONArray stationArray = decrptobj.getJSONArray("StationInfos");
JSONArray sortedstationArray = sortJsonArray(stationArray,"StationID"); //调用排序方法,对json数组中的对象排序
for (int j=0; j<sortedstationArray.size(); j++) {
row = sheet.createRow(rownum); //json数组中有多少对象就依次为每个对象创建一行
JSONObject bodyObj = sortedstationArray.getJSONObject(j);
for (int k=0; k<stationInfo_title.length; k++) {
cellContent = bodyObj.getString(stationInfo_title[k]);
cell = row.createCell(k); //创建单元格,即生成每行对应的列,新的一行每个单元格从0计数
cell.setCellValue(cellContent); //给每行的单元格赋值,组成每列的值
}
rownum ++;
} }catch (Exception e) {
e.printStackTrace();
} //创建一个文件
File file = new File("D:\\javaExample\\file\\stationInfo.xlsx");
try {
if(!file.exists()) {
file.createNewFile();
}
//创建输出流
OutputStream outputStream = new FileOutputStream(file);
//将拼好的内容通过输出流写到excle
workbook.write(outputStream);
//关闭输出流
outputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
} //专门写一个对JSONArray排序的方法
public static JSONArray sortJsonArray(JSONArray jsonArray, final String sortKey) {
List<JSONObject> list = new ArrayList<>();
for (int i=0; i<jsonArray.size(); i++) {
list.add(jsonArray.getJSONObject(i));
}
Collections.sort(list, new Comparator<JSONObject>() {
String key = sortKey;
@Override
public int compare(JSONObject o1, JSONObject o2) {
String str1 = o1.getString(key);
String str2 = o2.getString(key);
return str1.compareTo(str2);
}
});
//先清空原有数组
jsonArray.clear(); //将排序好的JSONObject放到JSONArray里
for (int j=0; j<list.size(); j++) {
jsonArray.add(list.get(j));
} return jsonArray;
}
}
最新文章
- 4、解析配置文件 redis.conf、Redis持久化RDB、Redis的主从复制
- append,appendTo和prepend #1daae2
- 网络流HDU 2883
- 一个.xib界面文件中设计有多个View
- 【特别推荐】8个富有创意的jQuery/CSS3插件
- 设置数据库为SINGLE_USER模式,减少锁定时间
- 2016年11月1日 星期二 --出埃及记 Exodus 19:17
- LaTex学习笔记
- Visual Studio 2010 更新NuGet Package Manager出错解决办法
- bootstrap01登录小例子
- Replace是替代 Split分割字符串
- 13.MySQL(一)
- 【dfs判负环】BZOJ1489: [HNOI2009]最小圈
- atom编辑器安装插件报错。。
- 14-Requests+正则表达式爬取猫眼电影
- Linux运维工程师必须掌握的基础技能有哪些?
- Android手势监听类GestureDetector的使用
- 20165327《Java程序设计》实验一 Java开发环境的熟悉 实验报告
- C++ string::size_type
- 2019-02-22 L231
热门文章
- Linux/Android——Input系统之frameworks层InputManagerService (六)【转】
- Android实战技巧之八:Ubuntu下切换JDK版本【转】
- Python基础第六天
- 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns
- 377 Combination Sum IV 组合之和 IV
- 22 C#中的异常处理入门 try catch throw
- 仿ofo单车做一个轮播效果
- Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1
- Apache ab使用指南
- mongodb Shell 启动