Convert ResultSet to JSON and XML
Posted YSHY
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Convert ResultSet to JSON and XML相关的知识,希望对你有一定的参考价值。
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception { JSONArray jsonArray = new JSONArray(); while (resultSet.next()) { int total_rows = resultSet.getMetaData().getColumnCount(); JSONObject obj = new JSONObject(); for (int i = 0; i < total_rows; i++) { obj.put(resultSet.getMetaData().getColumnLabel(i + 1) .toLowerCase(), resultSet.getObject(i + 1)); jsonArray.put(obj); } } return jsonArray; } public static String convertToXML(ResultSet resultSet) throws Exception { StringBuilder xmlArray = new StringBuilder("<results>"); while (resultSet.next()) { int total_rows = resultSet.getMetaData().getColumnCount(); xmlArray.append("<result "); for (int i = 0; i < total_rows; i++) { xmlArray.append(" ").append(resultSet.getMetaData().getColumnLabel(i + 1) .toLowerCase()).append("=‘").append(resultSet.getObject(i + 1)).append("‘"); } xmlArray.append(" />"); } xmlArray.append("</results>"); return xmlArray.toString(); }
以上是关于Convert ResultSet to JSON and XML的主要内容,如果未能解决你的问题,请参考以下文章
C# Newtonsoft.Json.JsonReaderException:“Could not convert string to decimal:
得到错误 org.json.JSONException: Value <pre of type java.lang.String cannot be convert to JSONObject
反序列化Newtonsoft.Json.JsonReaderException:“Could not convert string to decimal: . Path 'SETTLEAMT&
Convert String to NSURL is return nil in swift
*微妙* VB 函数和 Convert.To* 函数之间的区别?
Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree