Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:03:58+00:00 2026-06-12T13:03:58+00:00

i’m tying to implement a Servlet which generates a JSON the same way this

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:03:59+00:00Added an answer on June 12, 2026 at 1:03 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.