java - Outputting JSON -


i trying output list in json formatting it's not coming out quite way want it.

here output:

{     "songmap": [         {             "sid": "699",             "sdid": "1079287588763212246"         },         {             "sid": "700",             "sdid": "1079287588763212221"         },         {             "sid": "701",             "sdid": "1079287588763212230"         }     ] } 

here like:

[         {             "sid": "699",             "sdid": "1079287588763212246"         },         {             "sid": "700",             "sdid": "1079287588763212221"         },         {             "sid": "701",             "sdid": "1079287588763212230"         } ] 

i don't need nor want intermediate array, im not sure why it's being added. have custom class follows:

public class songid {     public int sid;     public string sdid;      public songid() {}      public songid(int key, string value) {         this.sid  = key;         this.sdid = value;     } } 

this how it's being serialized:

gson gson = new gson(); string json = gson.tojson(songmap); 

if print out string json ,it looks way want to. server adding additional "songmap." here how variable declared @ top of class:

private list<songid> songmap; ..... songmap = new arraylist<songid>(); 

it seems you're not serializing list directly container having list named songmap. (confusing name choice way.) if serialize list directly comes out fine shown below.

songmap = new arraylist<songid>(); songmap.add(new songid(699, "1079287588763212246")); songmap.add(new songid(700, "1079287588763212221"));  gsonbuilder gb = new gsonbuilder(); gson gson = gb.setprettyprinting().create();  system.out.println(gson.tojson(songmap)); 

output :

[   {     "sid": 699,     "sdid": "1079287588763212246"   },   {     "sid": 700,     "sdid": "1079287588763212221"   } ] 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -