4

Code of UserRequest class, which contains the list of users.

public class UserRequest {
    private List<User> userList;

    public UserRequest() {
    }

    public UserRequest(List<User> userList) {
        this.userList = userList;
    }

    public List<User> getUserList() {
        return this.userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

}

Code of User Class, which contains the id, first name and last name of the user.

public class User {

    private String id;
    private String firstName;
    private String lastName;

    public User() {
    }

    public User(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

I am using the GSON library, and the issue that I'm having is that my json request when serializing from java objects to json is not formatted in the way I need it to be.

The format of current situation

{"userList":[{"id":"12341234", "firstName": "Joeri", "lastName": "Verlooy"}]}

The format that is desirable:

[{"id":"12341234", "firstName": "Joeri", "lastName": "Verlooy"}]

Is there a way that I can send the plain array of json object without any name?

Joeri Verlooy
  • 613
  • 5
  • 19

3 Answers3

2

try to create a model for items (User) and then convert json to an java ArrayList. assume the json string is in strJson, then you can do it like below:

 ArrayList<User> lstItems = (new Gson()).fromJson(strJson, new TypeToken<ArrayList<User>>() {}.getType());

you dont actually need a model for the list of users (UserRequest), cuz the list doesnt have any name.

if you want to convert an object to a json including a list without a name do like below :

 ArrayList<User> lstUser = new ArrayList<User>();
 lstUser.add(new User());
 (new Gson()).toJson(lstUser, new TypeToken<ArrayList<User>>() {}.getType());
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
  • Isn't this for deserialization instead of serialization? I want to create the json from java objects, not the other way around. – Joeri Verlooy Apr 12 '17 at 09:00
  • if you want to serialize just do it with an arraylist of user model. then it will be changed into an array without name. – Amir Ziarati Apr 12 '17 at 09:26
  • You're missing one thing in `toJson` to make it more precise and align with the `fromJson` example better. – Lyubomyr Shaydariv Apr 12 '17 at 09:39
  • what am i missing ? – Amir Ziarati Apr 12 '17 at 09:43
  • @AmirZiarati Generic class instances (usually) cannot hold _declared_ generic class parameterization due to type erasure, thus simple `.toJson(object)` _can apply_ different serialization strategies to each list element. – Lyubomyr Shaydariv Apr 12 '17 at 09:52
  • i think you wont need to care about that when converting from object to string cuz i just tested different scenarios in this case and there was no problem. – Amir Ziarati Apr 12 '17 at 10:57
  • @AmirZiarati Suppose I have special type adapter for `List` (neither special `List` adapter, nor special `User` adapter). `.toJson(object)` will omit the special type adapter because of having not enough information regarding the _declared_ type for the given list. Compare these: https://google.github.io/gson/apidocs/com/google/gson/Gson.html#toJson-java.lang.Object- and https://google.github.io/gson/apidocs/com/google/gson/Gson.html#toJson-java.lang.Object-java.lang.reflect.Type- – Lyubomyr Shaydariv Apr 12 '17 at 11:24
  • sounds true . thanks alot bro. i thin k i must add the type token in to json as well . yes ? – Amir Ziarati Apr 13 '17 at 03:56
  • @AmirZiarati Yes, it would be merely better: by adding the type to the `toJson` method you make sure that your JSON read and write strategies use the same Gson read/write configuration in both directions. – Lyubomyr Shaydariv Apr 13 '17 at 14:47
1

To parse "custom" JSON using Gson library you need to use custom TypeAdapter where you'll be able to create list of Users object from unnamed array. Here you have example usage of TypeAdapter.

ostojan
  • 608
  • 1
  • 7
  • 17
  • Thanks for your response, I will look into the article. – Joeri Verlooy Apr 12 '17 at 09:01
  • 1
    I use custom type adapters in my project. [Here](https://github.com/ostojan/360toOne/blob/master/AndroidApp/app/src/main/java/com/ostojan/x360/model/GameTypeAdapter.java) and [here](https://github.com/ostojan/360toOne/blob/master/AndroidApp/app/src/main/java/com/ostojan/x360/model/RegionTypeAdapter.java) you can find my implementations. – ostojan Apr 12 '17 at 09:06
  • I don't think that the OP has to re-invent the overkill wheel: the OP seems just to get the list from the request object and convert it to JSON representation. – Lyubomyr Shaydariv Apr 12 '17 at 09:35
  • 1
    I agree that my solution might be overkill but I think it's more robust and "elegant". – ostojan Apr 12 '17 at 09:46
  • This solution worked like a charm, thanks! It might look like overkill but the fact that i can make a class that parses my data in an elegant way to the correct format is a big plus IMO. – Joeri Verlooy Apr 12 '17 at 11:48
0
JSONArray jsonArray = new JSONArray();
for (int i=0;i<userList.size();i++) {
    jsonArray.add(userList.get(i));
}

userList will be your list of users and here jsonArray will have the desirable format

Muhib Pirani
  • 765
  • 1
  • 6
  • 15
  • Did you check it? It cannot work: no [JsonArray overload](https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonArray.html#t2) can accept the OP's `User` class instance. – Lyubomyr Shaydariv Apr 12 '17 at 09:37