-2

次のような結果セットデータがあります。

+-------+-------+-------+-------+
| Code1 | Code2 | Code3 | Code4 |
+-------+-------+-------+-------+
|     1 |    11 |   111 |  1111 |
|     2 |    21 |   211 |  2111 |
|     2 |    22 |   221 |  2211 |
|     2 |    21 |   212 |  2121 |
+-------+-------+-------+-------+

上記の結果セットを次の2つのjsonに変換する必要があります。

code1_code2 = {
               1 : [11],
               2 : [21, 22, 21]
              };

code2_code3 = {
               11 : [111],
               21 : [211, 212],
               22 : [221]
              };

結果セットを解析しようとしましたが、最初の列が順序付けられているため、最初のjsonを取得できました。しかし、2番目の列が順序付けられていないため、2番目のjsonを取得できませんでした。

(注:-2番目の列は注文されません)

4

2 に答える 2

1

ノート

JSONの名前/値ペアの名前は文字列である必要があります(質問の数値ではありません)。

JSONオブジェクト
(ソース:json.org

サンプルコード

@josnidhinのコメントで述べたようにMap、結果セットが順序付けられているかどうかに関係なく、これらのデータを格納するために使用できます。

これがサンプルコードです。JSONのものを処理するためにjson-libを選択します。

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]
    };
于 2012-06-11T10:39:50.140 に答える
0

最初にツリー構造を構築する必要があります。次に、ツリーの各レベルで、JSONとして出力します。これは基本的にBFSのわずかな変更です(レベル順序トラバーサルとも呼ばれます)。

于 2012-06-11T08:43:13.720 に答える