list=dao.getList(hql);Gson gson = new Gson(); List med" />
您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> 请教一个gson转换的问题

请教一个gson转换的问题

来源:网络整理     时间:2016/7/1 13:35:25     关键词:

关于网友提出的“ 请教一个gson转换的问题”问题疑问,本网通过在网上对“ 请教一个gson转换的问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 请教一个gson转换的问题
描述:

本帖最后由 rookie926 于 2010-12-24 09:34:33 编辑

String hql="from VdDicMedicine where c17='1'";
List list=dao.getList(hql);
Gson gson = new Gson();
List medList = new ArrayList();   
for(int i=0;i<>
{
medicineList medBean = new medicineList();   
medBean.setName(list.get(i).getC02());
medBean.setPinyin(list.get(i).getC03());
medBean.setPrice(list.get(i).getC19()+"");
medBean.setStock(list.get(i).getC20()+"");
medList.add(medBean);
}  
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<>>() {}.getType(); 
Map totalMap = new HashMap();
System.out.println(gson.toJson(medList,type));
totalMap.put("total",239);
totalMap.put("rows", gson.toJson(medList,type));
System.out.println(gson.toJson(totalMap));
response.setCharacterEncoding("gbk");
response.getWriter().print(gson.toJson(totalMap));初用gson发现挺方便的,想获取一个json形式的数据,但是如上这么写的话,输入出来是这样的: 
{"total":239,"rows":"[{\"name\":\"青霉素\",\"pinyin\":\"QMS\",\"price\":\"4.19\"  
,\"stock\":\"20.0\"},{\"name\":\"苯唑西林\",\"pinyin\":\"BZXL\",\"price\":\"16.1  
9\",\"stock\":\"45.0\"},{\"name\":\"阿莫西林\",\"pinyin\":\"AMXL\",\"price\":\"2  
65.33\",\"stock\":\"0.0\"},{\"name\":\"氨苄西林\",\"pinyin\":\"ABXL\",\"price\":   
\"265.33\",\"stock\":\"0.0\"}]"}  
{"total":239,"rows":"[{\"name\":\"青霉素\",\"pinyin\":\"QMS\",\"price\":\"4.19\"
,\"stock\":\"20.0\"},{\"name\":\"苯唑西林\",\"pinyin\":\"BZXL\",\"price\":\"16.1
9\",\"stock\":\"45.0\"},{\"name\":\"阿莫西林\",\"pinyin\":\"AMXL\",\"price\":\"2
65.33\",\"stock\":\"0.0\"},{\"name\":\"氨苄西林\",\"pinyin\":\"ABXL\",\"price\":
\"265.33\",\"stock\":\"0.0\"}]"}
多了很多的"\",而且"rows":后面的内容也不需要被双引号引起来,似乎是嵌套上出了问题,应该改哪里呢? 

解决方案1:

package ExamProject.console.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ExamProject.console.jdbc.JDBCConnection;
import net.sf.json.JSONArray;
public class SelectAllServer extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("SelectAllServer");
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
JDBCConnection jb = new JDBCConnection();
int start = Integer.parseInt(req.getParameter("start"));
int limit = Integer.parseInt(req.getParameter("limit"));
AVInfo info;
String sql = "select count(*) from avactor";
ResultSet rs = jb.doQuery(sql);
int total = 0;
try {
if (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
int arrayIndex = 0;
int totalSelect = start + limit;
if (totalSelect > total) {// 简单解决数组问题
if (100 < totalSelect && totalSelect < 1000) {
arrayIndex = total % 100;
}
if (10 < totalSelect && totalSelect < 100) {
arrayIndex = total % 10;
}
} else {
arrayIndex = 10;
}
if (total < 10) {
arrayIndex = total;
}
String jieguo = "";
jieguo = "{totalProperty:" + total + ",";
jieguo += ("root:");
sql = "select * from avactor limit ?,?";
Object[] s = { start, limit };
rs = null;
rs = jb.doQuery(sql, s);
Object[] ob = new Object[arrayIndex];
int index = 0;
try {
while (rs.next()) {
info = new AVInfo();
info.setId(rs.getInt("id"));
info.setBWH(rs.getString("BWH"));
info.setAge(rs.getInt("age"));
info.setDescription(rs.getString("description"));
info.sethAndw(rs.getString("hAndw"));
info.setHeadImage(rs.getString("headImage"));
info.setName(rs.getString("name"));
ob[index] = info;
index++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
JSONArray json = JSONArray.fromObject(ob);
jieguo += json;
jieguo += "}";
PrintWriter pw = resp.getWriter();
System.out.println(jieguo);
pw.write(jieguo);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}

这是我用数组转换json的格式。楼主看下有用吗?
totalProperty:7,root:[{"BWH":"88-60-88","age":20,"description":"这只是一个女优反对广泛大概地方废话飞过海方法","hAndw":"160cm/50kg","headImage":"http://localhost:8080/AVManager/jsimg/user.gif","id":1,"name":"小泽武藤空"},{"BWH":"88-60-88","age":21,"description":"但是法国官方法国和规范华国锋覆盖","hAndw":"160cm/50kg","headImage":"http://localhost:8080/AVManager/jsimg/user.gif","id":2,"name":"小泽武藤空"},{"BWH":"88-60-88","age":22,"description":"各分店更好","hAndw":"160cm/50kg","headImage":"http://localhost:8080/AVManager/jsimg/user.gif","id":3,"name":"小泽武藤空"},{"BWH":"88-60-88","age":23,"description":"各分店","hAndw":"160cm/50kg","headImage":"http://localhost:8080/AVManager/jsimg/user.gif","id":4,"name":"小泽武藤空"},{"BWH":"88-56-88","age":22,"description":"发电各分店","hAndw":"160cm/50kg","headImage":"http://localhost:8080/AVManager/jsimg/user.gif","id":5,"name":"小泽武藤空"},{"BWH":"90-60-88","age":20,"description":"苍老师","hAndw":"160cm/50kg","headImage":"http://localhost:8080/ExamProject/image/avActorHeadImage/test.jpg","id":6,"name":"苍井空"},{"BWH":"88-50-88","age":20,"description":"吉泽老师","hAndw":"160cm/50kg","headImage":"http://localhost:8080/ExamProject/image/avActorHeadImage/199698b6291c1bc730add163.gif","id":7,"name":"吉泽明步"}]}
这是结果
以上介绍了“ 请教一个gson转换的问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2180707.html

相关图片

相关文章