简体   繁体   English

在Java Servlet中生成JSON对象

[英]Generate JSON Object in Java Servlet

i'm tying to implement a Servlet which generates a JSON the same way this php script does: 我想实现一个Servlet,该Servlet会以与该php脚本相同的方式生成JSON:

$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
/*
    $response->rows[$i]['id']=$row[id];
    $response->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
*/
    $response->rows[$i]['id']=$row['id'];
    $response->rows[$i]['name']=$row['name'];
    $response->rows[$i]['author']=$row['author'];
    //$response->rows[$i]=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
    $i++;
}        
echo json_encode($response);

the correct json output should be like this: 正确的json输出应如下所示:

{"page":null,"total":0,"records":"68","rows":[{"id":"1","name":"Thinking in Java","author":null},{"id":"4","name":"Effective Java: A Programming Language Guide","author":"Joshua Bloch"},{"id":"5","name":"Learn Java for Android Development","author":"Jeff Friesen"}]}

i tried it this way but it seems to generate another output: 我以这种方式尝试过,但似乎会产生另一个输出:

JsonArray jArray = new JsonArray();
    JsonObject jo = new JsonObject();
    jo.addProperty("page", page);
    jo.addProperty("total", totalPageString);
    jo.addProperty("records", count);
    jArray.add(jo);

    int i = 0;
    try {
        while (rs2.next()) {
            JsonObject tmp = new JsonObject();

            tmp.addProperty("rows[" + i + "]['lastname']", rs2.getString(1));
            tmp.addProperty("rows[" + i + "]['firstname']",
                    rs2.getString(2));
            tmp.addProperty("rows[" + i + "]['staffnumber']",
                    rs2.getString(3));
            jArray.add(tmp);
            i++;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    response.setContentType("application/Json");
    response.getWriter().print(jArray);

my output looks like this: 我的输出看起来像这样:

[{"page":"0","total":"Infinity","records":2},{"lastname":"Hanke","firstname":"Jannis","staffnumber":"9395835"},{"lastname":"Hanke","firstname":"Jannis","staffnumber":"83833"}]

rs2 is a resultset with data from database. rs2是包含数据库数据的结果集。 the php code is from the example page of jquery combogrid plugin. php代码来自jquery combogrid插件的示例页面。

i really dont get this working. 我真的没有得到这个工作。 can anybody help me please? 有人可以帮我吗?

Given the desired JSON, consider this: 给定所需的JSON,请考虑以下事项:

JsonObject jo = new JsonObject();
jo.addProperty("page", page);
jo.addProperty("total", totalPageString);
jo.addProperty("records", count);

JsonArray jArray = new JsonArray();
try {
    while (rs2.next()) {
        JsonObject row = new JsonObject();

        row.addProperty("id", rs2.getString(1));
        row.addProperty("name", rs2.getString(2));
        row.addProperty("author", rs2.getString(3));
        jArray.add(row);
    }
    jo.add("rows", jArray);
} catch (SQLException e) {
    e.printStackTrace();
}

response.setContentType("application/json");
response.getWriter().print(jo);

Important notes: 重要笔记:

  • because you have the rs2 resultset, rows disappears. 因为您有rs2结果集,所以rows消失了。 You won't want that as part of your field names. 您不希望将其作为字段名称的一部分。
  • it is likely that you will build the jArray and attach it to an object, rather than the other way around. 您很有可能会构建jArray并将其附加到对象,而不是相反。
  • see this link for the correct content-type 请参阅此链接以获取正确的内容类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM