package com.fjnu.json.util;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.fjnu.json.bean.Course;
import com.fjnu.json.dao.CourseDao;
import com.fjnu.json.dao.CourseDaoImpl;
/**
* 解析Json对象类
* @author nick
*
*/
public class JsonParse {
/**
* 从指定的中HttpServletRequest获取字节数组 *
* @param HttpServletRequest
* @return Json对象的String字符串(中文转码)
* @throws Exception
*/
public static String readParse2Str(HttpServletRequest req) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
InputStream inStream = null;
try {
// System.out.println("getCharacterEncoding: " + req.getCharacterEncoding());
// req.setCharacterEncoding("UTF-8");
// System.out.println("getCharacterEncoding: " + req.getCharacterEncoding());
inStream = req.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
// System.out.println("这里显示的中文为乱码 outStream.toString():"
// + outStream.toString());//这里显示的中文为乱码
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
String returnData = null;
try {
returnData = new String(outStream.toString().getBytes("gbk"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("已经经过转码,中文显示正常returnData: " + returnData);
return returnData;
}
/**
* 从指定的中HttpServletRequest获取字节数组 *
* @param HttpServletRequest
* @return 字节数组
* @throws Exception
*/
public static byte[] readParse(HttpServletRequest req) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
System.out.println("getCharacterEncoding: " + req.getCharacterEncoding());
req.setCharacterEncoding("utf-8");
System.out.println("getCharacterEncoding: " + req.getCharacterEncoding());
InputStream inStream = req.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
System.out.println("outStream.toString():"
+ outStream.toString());
}
inStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return outStream.toByteArray();
}
/**
* 解析Json数据, 方法可以根据需求自己设计
* @param urlPath
* @return mlists
* @throws Exception
*/
public static List<Course> getListCourse(HttpServletRequest req)
throws Exception {
List<Course> mlists = new ArrayList<Course>();
//byte[] data = readParse(req);
//要注意编码转换
//JSONArray array = JSONArray.fromObject(new String( new String(data).getBytes("gbk"),"utf-8"));
String data = readParse2Str(req);
//要注意编码转换
JSONArray array = JSONArray.fromObject(data);
//System.out.println("##########List<Course> getListCourse(HttpServletRequest req) ");
for (int i = 0; i < array.size(); i++) {
JSONObject item = array.getJSONObject(i);
Course course = new Course();
course.setId(item.getInt("id"));
course.setIndex(item.getInt("in_dex"));
course.setClasses(item.getString("classes"));
course.setLocation(item.getString("location"));
course.setTeacher(item.getString("teacher"));
course.setZhoushu(item.getString("zhoushu"));
course.setJieshu(item.getString("jieshu"));
course.setTime1(item.getString("time1"));
course.setTime2(item.getString("time2"));
course.setWhich(item.getString("which"));
course.setWeekType(item.getString("weekType"));
CourseDao courseDao = new CourseDaoImpl();
courseDao.updateCourse(course, item.getInt("tblNo"));
mlists.add(course);
}
return mlists;
}
}