java中的json解析

来源:互联网 发布:c语言 登录系统 编辑:程序博客网 时间:2024/06/04 08:41
@Override
public void getDoctor(){
List<Doctor> list=new ArrayList<Doctor>();
String json = loadJson("http://www.pkuih.edu.cn/page/emp!readList.html?empid=");
List<DoctorSub> list1=new ArrayList<DoctorSub>();
try {
JSONArray jsonArray = new JSONArray(json);
for(int i=0;i<jsonArray.length();i++){
Doctor doctor=new Doctor();
DoctorSub docSub=new DoctorSub();
JSONObject object = jsonArray.getJSONObject(i);

doctor.setName(object.getString("nameZh"));
//doctor.setTitle(array.get);
if(object.getJSONObject("sex")!=null){
if("男性".equals(object.getJSONObject("sex").getString("name"))){
doctor.setGender("1");
}else if("女性".equals(object.getJSONObject("sex").getString("name"))){
doctor.setGender("2");
}

}
//doctor.setGender(object.getString("sex"));
doctor.setPicture(object.getString("photoImg"));
doctor.setCode(object.getString("id"));
//doctor.setNameAcronym(object.getString("nameEn"));
doctor.setOrgCode("112628");
docSub.setOrgCode("112628");
if(object.getJSONObject("duty")!=null)
{doctor.setTitle(object.getJSONObject("duty").getString("name"));}

docSub.setDoctorCode(doctor.getCode());
doctor.setClinicName(object.getJSONObject("department").getString("nameEn"));
doctor.setClinicCode(object.getJSONObject("department").getString("departmentId"));
docSub.setMajorCode(object.getJSONObject("department").getString("departmentId"));
doctor.setServiceState(object.getString("workState"));
doctor.setRegistrationdate(DateFormater.parseSimpleDate(object.getString("created"), "yyyy-MM-dd"));
doctor.setPracticecatogary(object.getString("professionType"));
list.add(doctor);
list1.add(docSub);
}
doctorDao.batchInsert(list, null);
docotorSubDao.batchInsert(list1, null);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 




}
public String loadJson(String url) {
StringBuilder json=new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
String inputLine=null;
while((inputLine=in.readLine())!=null){
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
log.error("解析json的url异常");
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("读取流异常");
}
return json.toString();
}
}
0 0
原创粉丝点击