发布网友 发布时间:2022-04-24 14:30
共4个回答
热心网友 时间:2022-04-26 21:27
package Test2;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONProcess {
public JSONArray getJsonObject(String str){
JSONArray jo=JSONArray.fromObject(str);
return jo;
}
public void writeExcel(JSONArray ja) throws Exception{
Map map=new LinkedHashMap();
map.put("id", "id");
map.put("tag1", "tag1");
map.put("tag2", "tag2");
map.put("tag3", "tag3");
map.put("tag4", "tag4");
map.put("tag5", "tag5");
map.put("tag6", "tag6");
map.put("tag7", "tag7");
map.put("tag8", "tag8");
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格并设置表格默认列宽度为25个字节
HSSFSheet sheet = workbook.createSheet("sheet1");
sheet.setDefaultColumnWidth((short) 25);
// 生成表格标题
HSSFRow titleRow = sheet.createRow(0);
int columnLength1 = 20;
HSSFCell[] title = new HSSFCell[columnLength1];
for (int i = 0; i < columnLength1; i++) {
title[i] = titleRow.createCell((short) i);
title[i].setEncoding(HSSFCell.ENCODING_UTF_16);
title[i].setCellValue("");
}
// 生成表格表头行
HSSFRow headerRow1 = sheet.createRow(0);
HSSFCell[] header1 = new HSSFCell[columnLength1];
Iterator it=map.keySet().iterator();
int count=0;
while(it.hasNext()) {
header1[count] = headerRow1.createCell((short) count);
header1[count].setEncoding(HSSFCell.ENCODING_UTF_16);
header1[count].setCellValue(map.get(it.next()).toString());
count++;
}
// 遍历集合数据,产生数据行
FileOutputStream out = new FileOutputStream("E:/excelTest.xls");
int cellNum = 0;
if (ja != null) {
for (int rownum = 0; rownum < ja.size(); rownum++) {
HSSFRow row = sheet.createRow(rownum + 1);
JSONObject json = (JSONObject) ja.getJSONObject(rownum);
HSSFCell[] cell = new HSSFCell[columnLength1];
int counter=1;
cell[0] = row.createCell((short) 0);
cell[0].setEncoding(HSSFCell.ENCODING_UTF_16);
cell[0].setCellValue(json.get("id").toString());
JSONArray ja1 = (JSONArray) json.getJSONArray("tags");
for(int j=0;j<ja1.size();j++){
JSONObject json1=(JSONObject)ja1.get(j);
Iterator it1=json1.entrySet().iterator();
String key=it1.next().toString();
cell[counter] = row.createCell((short) counter);
cell[counter].setEncoding(HSSFCell.ENCODING_UTF_16);
cell[counter].setCellValue(key.split("=")[1]);
counter++;
}
}
}
workbook.write(out);
out.close();
}
public static void main(String[] args) {
JSONProcess jsonProcess=new JSONProcess();
String str="[{\"id\":1237869662,\"tags\":[{\"201201310009409169\":\"沙海\",\"weight\":\"123\"},{\"211007220000015286\":\"惊叹号\",\"weight\":\"117\"},{\"201110220006272797\":\"漫工厂\",\"weight\":\"5\"},{\"201102070003148762\":\"超好看\",\"weight\":\"28\"},{\"520\":\"出版人\",\"weight\":\"858\"},{\"2328\":\"作家\",\"weight\":\"5632\"},{\"4029\":\"盗墓笔记\",\"weight\":\"57495\"},{\"16115\":\"南派三叔\",\"weight\":\"754\"}]},{\"id\":29165812,\"tags\":[{\"34735\":\"学生一族\",\"weight\":\"1248\"},{\"12544\":\"热爱生活\",\"weight\":\"130120\"},{\"285\":\"音乐\",\"weight\":\"5374344\"},{\"983\":\"水瓶\",\"weight\":\"69854\"},{\"4\":\"美食\",\"weight\":\"19040573\"}]}]";
JSONArray ja=jsonProcess.getJsonObject(str) ;
try {
jsonProcess.writeExcel(ja);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
那是要我写了,我是有多闲啊。
其实这是json结构的字符串啊。在java里面很好处理啊,有现成的类处理这种字符串的。
放到txt或者excel也不难啊。但是代码量挺多的。
你需要的jar包。
commons-beanutils.jar
commons-collections.jar
commons-httpclient.jar
commons-lang.jar
commons-logging.jar
ezmorph-1.0.3.jar
json-lib-2.3-jdk15.jar
poi-2.5.1.jar
因为百度只能上传一个附件,其他jar包你自己弄吧。在百度搜索就行。
热心网友 时间:2022-04-26 22:45
//封装tag标签的内容
// 整个程序依赖 gson.jar
public class Tag {
private String timeStamp ;
private String name ;
private String weight ;
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
//封装一套信息的内容
import java.util.ArrayList;
import java.util.List;
public class NetWorkMessage {
private String id ;
private List<Tag> tags =new ArrayList<Tag>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
}
//操作类
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class Test {
public static void main(String[] args) throws Exception{
/**
* 该字符串不是标准的json,需要把 '201201310009409169':'沙海' 201201310009409169替换成'name',并将201201310009409169保存到 timestamp属性中
*/
String json = "[{'id':1237869662,'tags':[" +
"{\"201201310009409169\":\"沙海\",\"weight\":\"123\"}," +
"{\"211007220000015286\":\"惊叹号\",\"weight\":\"117\"}," +
"{\"201110220006272797\":\"漫工厂\",\"weight\":\"5\"}," +
"{\"201102070003148762\":\"超好看\",\"weight\":\"28\"}," +
"{\"520\":\"出版人\",\"weight\":\"858\"}," +
"{\"2328\":\"作家\",\"weight\":\"5632\"}," +
"{\"4029\":\"盗墓笔记\",\"weight\":\"57495\"}," +
"{\"16115\":\"南派三叔\",\"weight\":\"754\"}]}," +
"{\"id\":'29165812',\"tags\":[" +
"{\"34735\":\"学生一族\",\"weight\":\"1248\"}," +
"{\"12544\":\"热爱生活\",\"weight\":\"130120\"}," +
"{\"285\":\"音乐\",\"weight\":\"5374344\"}," +
"{\"983\":\"水瓶\",\"weight\":\"69854\"}," +
"{\"4\":\"美食\",\"weight\":\"19040573\"}]}]" ;
json = getJson(json);
List<NetWorkMessage> list = jsonToList(json);
/**
* 存储数据
*/
outputFile("D:/test/test.txt",list);
}
/**
* 将Json转换为对应的list
* @param json
* @return
*/
public static List<NetWorkMessage> jsonToList(String json){
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
TypeToken<List<NetWorkMessage>> token = new TypeToken<List<NetWorkMessage>>(){};
return gson.fromJson(json,token.getType() );
}
/**
* 将页面抓取到的不标准的 Json串转换为标准的Json串
* @param str 页面赚取到的值
* @return
*/
public static String getJson(String str){
String regex = "['\"]?([\\d]+)['\"]?\\s*[:]\\s*['\"]?([\u4E00-\u9FA5]{1,})['\"]?";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while(m.find()){
StringBuilder s = new StringBuilder();
s.append("'name':'").append(m.group(2)).append("','timeStamp':'").append(m.group(1)).append("'");
m.appendReplacement(sb,s.toString());
}
m.appendTail(sb);
return sb.toString();
}
/**
* 将内存中的数据存放的指定文件中
* @param file 存放位置,全路径
* @param data 内存数据
* @throws Exception
*/
public static void outputFile(String filename,List<NetWorkMessage> data)throws Exception{
File file = new File(filename); file.getParentFile().mkdirs();
BufferedWriter br = new BufferedWriter(new FileWriter(file, false));
PrintWriter out = new PrintWriter(br);
/**
* 获取tags最大的值
*/
int maxSize = 0 ;
for(NetWorkMessage msg : data){
String id = msg.getId() ;
List<Tag> tags = msg.getTags();
maxSize = maxSize < tags.size()?tags.size():maxSize ;
}
/**
* 存储表头数据
*/
out.print("id\t\t");
for(int i=1; i<=maxSize ; i++){
out.print("tags"+i+"\t\t");
}
out.print("\n");
out.flush();
/**
* 存储对应数据
*/
for(NetWorkMessage msg : data){
String id = msg.getId() ;
out.print(id+"\t\t");
List<Tag> tags = msg.getTags();
for(Tag tag : tags){
out.print(tag.getName()+"\t\t");
}
out.print("\n");
out.flush();
}
out.close();
out = null;
}
}
运行 test后结果如下
id tags1 tags2 tags3 tags4 tags5 tags6 tags7 tags8
1237869662 沙海 惊叹号 漫工厂 超好看 出版人 作家 盗墓笔记 南派三叔
29165812 学生一族 热爱生活 音乐 水瓶 美食
排版可能没有对齐
热心网友 时间:2022-04-27 00:20
这类字符串是JSON格式,你可以去网上下载json相关的jar包,或者关键词搜索java json就有很多办法可以做到。
热心网友 时间:2022-04-27 02:11
JSON格式的数据吧,可以先在网上看下相关JSON的