单个对象获取json
ShuangjiamessForm result = new ShuangjiamessForm();
通过caseid获取json串
PxDeviceData bean = deviceDataDAO.getload(caseId);if (bean != null) {
// PxDeviceData bean = list.get(0);
// System.out.println(bean.getCaseType());
try {
这里取出json字符串
JSONObject object = new JSONObject(bean.getCaseJson());
如果就是第一个字符串里面的通过下面这种方式获取
String machineId =object.getString("MachineId");
因为字符串里面又一个字符串因此通过前面的名称获取里面的字符串
JSONObject mem = object.getJSONObject("Member");
定义一个变量来获取字符串 并打印到java后台
String name = mem.getString("Name");System.out.println(name);
这里的isnull是判断height是否有数据
// System.out.println("height: "+object.isNull("Height"));boolean height1 = object.isNull("Height");
System.out.println(height1);
这里判断如果不是空 就获取字符串信息
if (height1 != true) {JSONObject sg = object.getJSONObject("Height");
String height = sg.getString("Height");
System.out.println("ssss" + height);
String weight = sg.getString("Weight");
String bmi = sg.getString("BMI");
result.setHeight(height);
result.setWeight(weight);
result.setBmi(bmi);
}
通过迭代器遍历输出caseid和日期epoch
//shuangjiamessFrom是一個类 里面有各个属性
List<ShuangjiamessForm> result = new ArrayList<ShuangjiamessForm>();
//pxDeviveDate是数据实体类
它实现了查询listbyUser里面的sql语句
List<PxDeviceData> list = deviceDataDAO.getListByUser(userId);
Iterator<PxDeviceData> it = list.iterator();
while(it.hasNext())
{
PxDeviceData bean = it.next();
try {
form.setCaseid(bean.getCaseId());
String n=bean.getCaseId();
System.err.println(n);
form.setEpoch(bean.getEpoch());
Date epoch=bean.getEpoch();
System.out.println(epoch);
result.add(form);
} catch (JSONException e) {
e.printStackTrace();
}
}
对应的查询sql方法
public List<PxDeviceData> getListByUser(String userId)
{
List<PxDeviceData> result = new ArrayList<PxDeviceData>();
try {
String queryString = "select pdd.caseId, pdd.epoch from PxDeviceData pdd where pdd.userId = ? ";
Query query = getSession().createQuery(queryString);
query.setParameter(0, userId);
result = query.list();
// System.out.println(result);
} catch (RuntimeException re) {
throw re;
}
return result;
}
下面是简化过的
public List<ShuangjiamessForm> getListByUser(String userId) {
//shuangjiamessFrom是一個类 里面有各个属性
List<ShuangjiamessForm> result = new ArrayList<ShuangjiamessForm>();
//list是一个数组 因此是boject[] 后来获取 就直接获取数组第一个是 id第二个是日期
List list = deviceDataDAO.getListByUser(userId);
Iterator it = list.iterator();
while(it.hasNext())
{
Object[] bean = (Object[])it.next();
String caseid = bean[0].toString();
Date date = (Date)bean[1];
ShuangjiamessForm form = new ShuangjiamessForm();
form.setCaseid(caseid);
form.setEpoch(date);
// System.out.println(date);
result.add(form);
}
return result;
}
}
同时sql也改
public List<PxDeviceData> getListByUser(String userId)
{
List result = null;
try {
String queryString = "select pdd.caseId, pdd.epoch from PxDeviceData pdd where pdd.userId = ? ";
Query query = getSession().createQuery(queryString);
query.setParameter(0, userId);
result = query.list();
// System.out.println(result);
} catch (RuntimeException re) {
throw re;
}
return result;
}
}