专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Web开发

请问一个gson转换的有关问题

发布时间:2010-05-31 07:02:58 文章来源:www.iduyao.cn 采编人员:星星草
请教一个gson转换的问题
String hql="from VdDicMedicine where c17='1'";
List<VdDicMedicine> list=dao.getList(hql);

Gson gson = new Gson();
List<medicineList> medList = new ArrayList<medicineList>();  

for(int i=0;i<list.size();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<List<medicineList>>() {}.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":后面的内容也不需要被双引号引起来,似乎是嵌套上出了问题,应该改哪里呢? 



------解决方案--------------------
Java code
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);
    }

}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: