java - mapping resultset to json -


i have resultset data follows :

+-------+-------+-------+-------+ | code1 | code2 | code3 | code4 | +-------+-------+-------+-------+ |     1 |    11 |   111 |  1111 | |     2 |    21 |   211 |  2111 | |     2 |    22 |   221 |  2211 | |     2 |    21 |   212 |  2121 | +-------+-------+-------+-------+ 

i need above result set converted following 2 jsons.

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

i have tried parsing result set , 1st column ordered 1st json. second column not ordered couldnot 2nd json.

(note:- not getting 2nd column ordered)

note

the name in name/value pair of json should string (not number in question).

json object http://www.json.org/object.gif

sample code

as mentioned in @josnidhin 's comment, can use map store these data no matter if result set ordered or not.

here's sample code, choose json-lib handle json stuff.

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 ())         (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 ();     } } 

sample output

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

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -