笔记
JSON的名称/值对中的名称应该是字符串(不是您问题中的数字)。
(来源:json.org)
示例代码
正如@josnidhin 的评论中提到的,Map
无论结果集是否有序,您都可以使用它来存储这些数据。
这是一个示例代码,我选择json-lib来处理 JSON 的东西。
package test;
import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import net.sf.json.*;
public class StackOverflowQ10976771_ResultSetToJSON
{
public static void appendValue (Map<String, List<Integer>> map, String key, int value)
{
List<Integer> values = map.get (key);
if (values == null)
{
values = new ArrayList<Integer> ();
map.put (key, values);
}
values.add (value);
}
public static void main (String[] args)
{
Map<String, List<Integer>> code1_code2 = new ConcurrentSkipListMap<String, List<Integer>> ();
Map<String, List<Integer>> code2_code3 = new ConcurrentSkipListMap<String, List<Integer>> ();
Map<String, List<Integer>> code3_code4 = new ConcurrentSkipListMap<String, List<Integer>> ();
int[][] sample_resultSet = {
{1, 11, 111, 1111},
{2, 21, 211, 2111},
{2, 22, 221, 2211},
{2, 21, 212, 2121},
};
//ResultSet rs = null;
//while (rs.next ())
for (int[] rs : sample_resultSet)
{
appendValue (code1_code2, String.valueOf(rs[0]), rs[1]);
appendValue (code2_code3, String.valueOf(rs[1]), rs[2]);
appendValue (code3_code4, String.valueOf(rs[2]), rs[3]);
}
System.out.println ("code1_code2 =");
System.out.println (JSONObject.fromObject (code1_code2).toString(4 ,4) + ";");
System.out.println ();
System.out.println ("code2_code3 = ");
System.out.println (JSONObject.fromObject (code2_code3).toString(4 ,4) + ";");
System.out.println ();
//System.out.println ("code3_code4 = ");
//System.out.println (JSONObject.fromObject (code3_code4).toString(4 ,4) + ";");
//System.out.println ();
}
}
样本输出
code1_code2 =
{
"1": [11],
"2": [
21,
22,
21
]
};
code2_code3 =
{
"11": [111],
"21": [
211,
212
],
"22": [221]
};