0

i'm tying to implement a Servlet which generates a JSON the same way this php script does:

$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:

{"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. the php code is from the example page of jquery combogrid plugin.

i really dont get this working. can anybody help me please?

J-H
  • 1,492
  • 4
  • 20
  • 36
  • 1
    You should probably add JSON snippets generated by both the PHP and Java code. It will be easier to diagnose if people can see an example of "good" and "bad" JSON output. – Guido Simone Oct 03 '12 at 23:15

1 Answers1

1

Given the desired JSON, consider this:

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. 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.
  • see this link for the correct content-type
Community
  • 1
  • 1
Michael Easter
  • 21,462
  • 7
  • 70
  • 97
  • thanks for your help so far. i'm using gson from google. there is no method like addProperty(String,Object) only addProperty with one parameter. – J-H Oct 03 '12 at 23:23
  • I've modified the code to have jo.add("rows", jArray). that should work – Michael Easter Oct 03 '12 at 23:35
  • I've also edited to reflect the desired JSON output. This example assumes that the SQL against the database is requesting columns id,name,author in that order – Michael Easter Oct 03 '12 at 23:40
  • 1
    Thank you so much for your help. It is working... Awesome :) – J-H Oct 03 '12 at 23:50