0

I am trying to output a list in JSON formatting but it's not coming out quite the way I want it.

Here is the output:

{
    "songMap": [
        {
            "SID": "699",
            "SDID": "1079287588763212246"
        },
        {
            "SID": "700",
            "SDID": "1079287588763212221"
        },
        {
            "SID": "701",
            "SDID": "1079287588763212230"
        }
    ]
}

Here is what I would like it to look like:

[
        {
            "SID": "699",
            "SDID": "1079287588763212246"
        },
        {
            "SID": "700",
            "SDID": "1079287588763212221"
        },
        {
            "SID": "701",
            "SDID": "1079287588763212230"
        }
]

I don't need nor want the intermediate array, im not sure why it's being added. I have a custom class as 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 is how it's being serialized:

Gson gson = new Gson();
String json = gson.toJson(songMap);

If I print out the string json ,it looks the way I want it to. However the server is adding the additional "SongMap." Here is how my variable is declared at the top of the class:

private List<songID> songMap;
.....
songMap = new ArrayList<songID>();
Paul Podgorsek
  • 2,416
  • 3
  • 19
  • 22
john cs
  • 2,220
  • 5
  • 32
  • 50
  • songMap itself, which contains many songID objects – john cs Aug 24 '13 at 04:45
  • possible duplicate of [JAX-RS - JSON without root node](http://stackoverflow.com/questions/4838192/jax-rs-json-without-root-node) – Thilo Aug 24 '13 at 04:46
  • There seem to be (non-portable) ways to configure JAX-RS to not do that (see the linked other question), but if you control the client, too, it is probably easier to just unwrap the response on the client. – Thilo Aug 24 '13 at 04:47
  • @Thilo thank you for the link, it's not a huge deal, I can just manage it on the client, just thought it was curious behavior. If you create an answer with this solution/link, I will give you credit. – john cs Aug 24 '13 at 04:54

1 Answers1

0

It seems you're not serializing the List directly but a container having the List named as songMap. (Confusing name choice by the way.) If you serialize the List directly it comes out fine as 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"
  }
]
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Right, I receive that response when I print out gson as I mentioned above, the problem comes when the server sends the response. I get the above output when analyzing it with Fiddler. – john cs Aug 24 '13 at 04:52
  • What you probably need is `gson.toJson(serverResponse.getSongMap());` instead of serializing `serverResponse` directly. That's psuedo-code by the way. I would need to see your code to be exact. – Ravi K Thapliyal Aug 24 '13 at 04:55