反正最近就一直在处理流。。。这次写的是给一个url,跳到一个json串页面。。。然后·,将这个json串数据写入csv文件。。。具体要求就是,把符合条件的"features" 下的"attributes"写入csv文件。
public static void jsonWrite(String urlString, String filePath) {
BufferedWriter writer = null;
InputStreamReader isr = null;
BufferedReader br = null;
HttpURLConnection conn = null;
StringBuffer json = new StringBuffer();
try {
writer = new BufferedWriter(new FileWriter(new File(filePath)));
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
isr = new InputStreamReader(conn.getInputStream());
br = new BufferedReader(isr);
String line;
if ((line = br.readLine()) != null) {
json.append(line); //将json数据获取下来
}
JSONObject data = JSONObject.fromObject(json.toString()); //将json整个数据转换成JSONObject 格式
JSONArray features = data.getJSONArray("features"); //获取json这个JSONObject中的features,features是个JSONArray
JSONArray selectedArray = new JSONArray();
int countHead = 0;
StringBuffer head = new StringBuffer();
if (features.size() > 0) {
for(int j = 0;j<features.size();j++){
JSONObject feature1 = features.getJSONObject(j);
JSONObject attribute1 = feature1==null?null:feature1.getJSONObject("attributes");
String majorStatusValue = attribute1==null?"":attribute1.get("MAJOR_STATUS").toString();
//读取配置文件,获取需要的majorStatus List<String> majorStatusList = readProperties("major_status.properties", "MAJOR_STATUS");
for(String majorStatus:majorStatusList){ //从全部的features中筛选符合条件的features,放到新的JSONArray中
if(majorStatusValue.equals(majorStatus)){
selectedArray.add(feature1);
break;
}
}
}
for (int i = 0; i < selectedArray.size(); i++) {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
JSONObject feature = selectedArray.getJSONObject(i);
JSONObject attributesObj = feature.getJSONObject("attributes");
System.out.println(attributesObj.toString());
Iterator it = attributesObj.keys();
while (it.hasNext()) {
String key = (String) it.next();
Object value = attributesObj.get(key);
if (key.equals("last_edited_date") || key.equals("created_date")) { //需要把long型的时间转换成date类型
Object valueObj = attributesObj.get(key); //因为value的类型不确定
if (valueObj instanceof Long) { // valueObj 类型是Long,就转换成date类型,如果为null,就直接放进去
Long time = Long.parseLong(dealWithData(valueObj.toString()));
Date date = new Date(time);
SimpleDateFormat sd = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
value = sd.format(date);
}
}
map.put(key, value); //一个 attributes数据以key-value形式放入map
}
for (String key : map.keySet()) {
countHead++; //写入表头,也就是写入前25个(25列)
if (countHead > 25)
break;
head.append(dealWithData(key)).append(",");
}
if (countHead == 25) { //写完第一行表头,换行,只有第一个map的key写进去,其他map的key不写进去
writer.write(head.toString());
writer.newLine();
}
int countValue = 0;
for (Object value : map.values()) {
StringBuffer values = new StringBuffer();//这里values是一个map的value拼在一起
values.append(dealWithData(value.toString())).append(
",");
countValue++;
writer.write(values.toString());//将一个map的数据写入BufferedWriter ,也就是一个 attributes数据
if (countValue % 25 == 0) { //写完一个map都要换行
writer.newLine();
}
}
}
}
writer.close(); //不要忘记
} catch (Exception e) {
e.printStackTrace();
}
}