Java database resultset to JSON implementation

Source: Internet
Author: User

There are now a lot of JSON-related Java tools, such as Json-lib, Gson, and so on, which can convert javabean directly into JSON format.

In development, it is possible to get data from the database and want to go directly to the JSON array, not through the bean.

For example, perform the following conversions:

Data sheet:

Id

Name

Age

1

Xxg

23

2

Xiaoming

20

Convert to JSON array:

[
            {
                "id": "1",
                 "name": "Xxg",
                 "Age": "All"
            },
            {
                "id": "2",
                 "name": "Xiaoming",
                 "Age": "
"             }
]

The implementation is simple, that is, the query results resultset every piece of data into a JSON object, the column name and value of each column in the data form a key-value pair, placed in the object, and finally organized into a JSON array.

[Java] view Plaincopyprint?

  1. Public String Resultsettojson (ResultSet rs) throws Sqlexception,jsonexception
  2. {
  3. JSON array
  4. Jsonarray array = new Jsonarray ();
  5. Get Number of columns
  6. ResultSetMetaData metaData = Rs.getmetadata ();
  7. int columnCount = Metadata.getcolumncount ();
  8. Traverse each piece of data in the ResultSet
  9. while (Rs.next ()) {
  10. Jsonobject jsonobj = new Jsonobject ();
  11. Iterate through each column
  12. for (int i = 1; I <= columnCount; i++) {
  13. String columnName =metadata.getcolumnlabel (i);
  14. String value = rs.getstring (ColumnName);
  15. Jsonobj.put (columnName, value);
  16. }
  17. Array.put (Jsonobj);
  18. }
  19. return array.tostring ();
  20. }
    Public String Resultsettojson (ResultSet rs) throws Sqlexception,jsonexception    {       //JSON array       jsonarray array = new Jsonarray ();             Gets the number of columns       resultsetmetadata metaData = Rs.getmetadata ();       int columnCount = Metadata.getcolumncount ();             Traverse each data in the ResultSet        while (Rs.next ()) {            Jsonobject jsonobj = new Jsonobject ();                       Traverse each column for            (int i = 1; I <= columnCount; i++) {                String columnName =metadata.getcolumnlabel (i);                String value = rs.getstring (columnName);                Jsonobj.put (columnName, value);            }             Array.put (jsonobj);         }             return array.tostring ();    }

The above code only needs to use the Org.json jar package, which can be downloaded anywhere on the web.

Fork Brother reproduced please indicate the source: http://blog.csdn.net/xiao__gui/article/details/8612503

Java database resultset to JSON implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.