Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using Gson library but when it serializes the JsonArray object, it seems to serialize this as an object rather than a JSON array. i.e.

{ elements: [ {name:"value1}, {name:"value2"}]}

How do I remove the elements from being serialized?

share|improve this question
    
Are you working with the JsonArray directly, instead of just using the data binding API of Gson, in order to customize the output, or for some other shortcoming of the data binding API that doesn't suit your needs? (It's less usual to use the JsonElements than to just use the simple data binding API, which is where Gson excels.) –  Programmer Bruce Apr 3 '13 at 5:45

1 Answer 1

I went to see the doctor, because my foot hurt when I walked on it. The doctor said, "Don't walk on it."

Generally, when working with an API like Gson, one would rather not even know that JsonArray exists, and they'd instead just use the data binding part of the API. So, instead of manually building a JsonArray, and then deal with serializing it, just feed a Java List or array to Gson.toJson(). For example:

List list = new ArrayList();
list.add("one");
list.add(2);
list.add(new Foo());

Gson gson = new Gson();
String json = gson.toJson(list);
System.out.println(json);

If that approach doesn't fit your needs and you're stuck using a JsonArray for some reason, then you might be tempted to just call its toString() method, since that does currently create what's ultimately desired, I wouldn't use it, because there is nothing in the documentation that says the toString() is guaranteed to create a valid JSON representation of the enclosed array contents. So, it might not return a String of the same format in future releases of Gson.

At any rate, if you really want to use a JsonArray, it should serialize well enough as follows.

JsonElement one = new JsonPrimitive("one");
JsonElement two = new JsonPrimitive(2);
JsonObject foo = new JsonObject();
foo.addProperty("foo", new Foo().foo);

JsonArray jsonArray = new JsonArray();
jsonArray.add(one);
jsonArray.add(two);
jsonArray.add(foo);

System.out.println(new Gson().toJson(jsonArray));
// ["one",2,{"foo":"hi"}]

Note: This answer is based on the Gson 2.2 API. I don't recall whether earlier versions of Gson included the overloaded toJson(JsonElement) methods.

If the toJson method is already being used in this fashion (to serialize a JsonArray), but the output is as demonstrated in the original question, recall that Java doesn't consider the runtime type when selecting amongst overloaded methods. It binds to the compile time type. (Lame -- I know.) So, you may need to cast the argument type to JsonElement, to let the compiler know which method to bind to. The following demonstrates what might be effectively happening in the original question.

System.out.println(new Gson().toJson((Object)jsonArray));
// {"elements":["one",2,{"foo":"hi"}]}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.